Java.lang Package Classes

Java Short - valueOf() Method



The java.lang.Short.valueOf() method returns a Short object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed short in the radix specified by the second argument, exactly as if the argument were given to the parseShort(java.lang.String, int) method. The result is a Short object that represents the short value specified by the string.

In other words, this method returns a Short object equal to the value of: new Short(Short.parseShort(s, radix)).

Syntax

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

Parameters

s Specify the string to be parsed.
radix Specify the radix to be used in interpreting s.

Return Value

Returns a Short object holding the value represented by the string argument in the specified radix.

Exception

Throws NumberFormatException, if the String does not contain a parsable short.

Example:

In the example below, the java.lang.Short.valueOf() method returns a Short object holding the value given by the specified String and parsed with the specified radix.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating a string holding short value
    String x = "100";
    String y = "6F";

    //creating Short object using radix as 2 (binary)
    Short p = Short.valueOf(x, 2);

    //creating Short object using radix as 16 (hexadecimal)
    Short q = Short.valueOf(y, 16);

    //printing the string
    System.out.println("The string x is: " + x); 
    System.out.println("The string y is: " + y); 

    //printing the Short object 
    System.out.println("The Short object p is: " + p);   
    System.out.println("The Short object q is: " + q);    
  }
}

The output of the above code will be:

The string x is: 100
The string y is: 6F
The Short object p is: 4
The Short object q is: 111

❮ Java.lang - Short