Java.lang Package Classes

Java Short - valueOf() Method



The java.lang.Short.valueOf() method returns a Short instance representing the specified short value. If a new Short instance is not required, this method should generally be used in preference to the constructor Short(short), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

Syntax

public static Short valueOf(short s)

Parameters

s Specify a short value.

Return Value

Returns a Short instance representing s.

Exception

NA.

Example:

In the example below, the java.lang.Short.valueOf() method returns a Short instance representing the given short value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating short value
    short x = 25;

    //creating Short instance
    Short y = Short.valueOf(x);

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

    //printing the Short instance 
    System.out.println("The Short instance is: " + y);   
  }
}

The output of the above code will be:

The short value is: 25
The Short instance is: 25

❮ Java.lang - Short