Java.lang Package Classes

Java Integer - valueOf() Method



The java.lang.Integer.valueOf() method returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), 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 Integer valueOf(int i)

Parameters

i Specify an int value.

Return Value

Returns a Integer instance representing i.

Exception

NA.

Example:

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

import java.lang.*;

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

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

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

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

The output of the above code will be:

The int value is: 25
The Integer instance is: 25

❮ Java.lang - Integer