Java.lang.Short.valueOf() Method

Advertisements


Description

The java.lang.Short.valueOf(String s, int radix) method returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument.

Declaration

Following is the declaration for java.lang.Short.valueOf() method

public static Short valueOf(String s, int radix) throws NumberFormatException

Parameters

  • s -- This is the string to be parsed.

  • radix -- This is the radix to be used in interpreting s

Return Value

This method returns a Short object holding the value represented by the string argument in the specified radix.

Exception

  • NumberFormatException -- If the String does not contain a parsable short.

Example

The following example shows the usage of java.lang.Short.valueOf() method.

package com.tutorialspoint;

import java.lang.*;

public class ShortDemo {

   public static void main(String[] args) {

     short shortNum = 100;
     String str = "1000";
    
     // returns Short object representing given short value
     Short ShortValue = Short.valueOf(shortNum);
     // displays the short object value
     System.out.println("Short object representing the specified short value =
     " + ShortValue);
     
     // returns a Short object holding the value given by the specified String
     ShortValue = Short.valueOf(str); 
     // displays the short object value
     System.out.println("Short object holding the value given by the specified
     String = " + ShortValue); 
   
     /* returns a Short object holding the value from the specified String
     according to radix. */
     ShortValue = Short.valueOf(str , 2) ;
     // displays the short object value
     System.out.println("Short object value for specified String with radix 2
     =" + ShortValue);
    
     // returns a Short object holding the value for string to radix   
     ShortValue = Short.valueOf("15" , 8) ;
     // displays the short object value
     System.out.println("Short object value String 15 with radix 8
     = " + ShortValue);
  }
}

Let us compile and run the above program, this will produce the following result:

Short object representing the specified short value = 100
Short object holding the value given by the specified String = 1000
Short object value for specified String with radix 2 = 8
Short object value String 15 with radix 8 = 13



Advertisements
Advertisements