Java.lang Package Classes

Java Integer - valueOf() Method



The java.lang.Integer.valueOf() method returns an Integer object holding the value of the specified String. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method. The result is an Integer object that represents the integer value specified by the string.

In other words, this method returns an Integer object equal to the value of: new Integer(Integer.parseInt(s)).

Syntax

public static Integer valueOf(String s)
                       throws NumberFormatException

Parameters

s Specify the string to be parsed.

Return Value

Returns a Integer object holding the value represented by the string argument.

Exception

Throws NumberFormatException, if the string cannot be parsed as an integer.

Example:

In the example below, the java.lang.Integer.valueOf() method returns a Integer object holding the value given by the specified String.

import java.lang.*;

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

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

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

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

The output of the above code will be:

The string is: 25
The Integer object is: 25

❮ Java.lang - Integer