New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Java Integer valueOf(String s) Method

Java valueOf(String s) method is a part of the Integer class of the java.lang package. This method is used to return the Integer object of the string value passed as an argument.

It must be noted that the argument is treated as a signed decimal integer and the value returned by this method can be interpreted as new Integer(Integer.parseInt(s)).

Syntax:

public static Integer valueOf(String s) throws NumberFormatException  

Parameters:

The parameter passed is the string whose equivalent Integer object is to be returned.

Exception:

NumberFormatException: This exception occurs when the input string is not parsable.

Returns:

Returns the Integer object of the String value passed as parameter.

Example 1:

Here, the Integer object is returned of the String value passed.

import java.lang.Integer;

public class StudyTonight 
{  
  public static void main(String[] args)
    {  
       String s1 = "909";
       String s2 = "253";
       System.out.println("Equivalent Integer object Value = " + Integer.valueOf(s1));//returns a Integer object representing the String specified 
       System.out.println("Equivalent Integer object Value = " + Integer.valueOf(s2));//returns a Integer object representing the String specified    
             
    }  
} 


Equivalent Integer object Value = 909
Equivalent Integer object Value = 253

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.lang.Integer;
import java.util.Scanner;

public class StudyTonight 
{  
  public static void main(String[] args)
  {    
    try
     {
      System.out.println("Enter the string value");
      Scanner sc=new Scanner(System.in);
      String x = sc.next();
      System.out.println("Equivalent Integer object Value = " + Integer.valueOf(x));//returns a Integer object representing the string specified 
      }
     catch(NumberFormatException e)
      {
      System.out.println("Invalid input!!");
      }
   }  
} 


Enter the string value
610
Equivalent Integer object Value = 610
*************************************
Enter the string value
0x812
Invalid input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.