Signup/Sign In

Java Integer valueOf(int i) Method

Java valueOf(int i) method is part of the Integer class of the java.lang package. This method is used to return the equivalent Integer object of the integer primitive value passed as an argument.

Syntax:

public static Integer valueOf(int i)  

Parameters:

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

Returns:

Returns the Integer object of the integer value passed as a parameter.

Example 1:

Here, the Integer object representations are returned of the integer value passed.

import java.lang.Integer;

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


Equivalent Integer object Value = 211
Equivalent Integer object Value = -533

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 value");
       Scanner sc=new Scanner(System.in);
       int x = sc.nextInt();
       System.out.println("Equivalent Integer object Value = " + Integer.valueOf(x));//returns a Integer object representing the integer specified 
      }
     catch(Exception e)
      {
        System.out.println("Invalid input!!");
      }
   }  
} 


Enter the value
41
Equivalent Integer object Value = 41
*****************************************
Enter the value
0x481
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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.