- The java.lang.Integer.valueOf(int a) is an inbuilt method which is used to return an Integer instance representing the specified int value a.
Syntax :
public static Integer valueOf(int a)
Parameters : The method accepts a single parameter a of integer type representing the parameter whose Integer instance is to be returned.
Return Value : The method returns an Integer instance representing a.
Examples :
Input: a = 65 Output: 65 Input: a = -985 Output: -985
Below programs illustrate the java.lang.Integer.valueOf(int a) method.
Program 1: For a positive number.// Java praogram to illustrate the// java.lang.Integer.valueOf(int a)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){Integer obj =newInteger(10);// Returns an Integer instance// representing the specified int valueSystem.out.println("Output Value = "+obj.valueOf(85));}}Output:Output Value = 85
Program 2: For a negative number.
// Java praogram to illustrate the// java.lang.Integer.valueOf(int a)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){Integer obj =newInteger(10);// It will return a Integer instance// representing the specified int valueSystem.out.println("Output Value = "+obj.valueOf(-9185));}}Output:Output Value = -9185
- The java.lang.Integer.valueOf(String str) is an inbuilt method which is used to return an Integer object, holding the value of the specified String str.
Syntax:
public static Integer valueOf(String str)
Parameters: This method accepts a single parameter str of String type that is to be parsed.
Return Value: The method returns an Integer object holding the value represented by the string argument.
Examples:
Input: str = "65" Output: 65 Input: str = "-452" Output: 452
Below programs illustrate the java.lang.Integer.valueOf(String str) method:
Program 1: For a positive number.// Java praogram to illustrate the// java.lang.Integer.valueOf(String str)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){Integer obj =newInteger(8);String str ="424";// It will return a Integer instance// representing the specified stringSystem.out.println("Integer Value = "+obj.valueOf(str));}}Output:Integer Value = 424
Program 2: For a negative number.
// Java praogram to illustrate the// java.lang.Integer.valueOf(String str)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){Integer obj =newInteger(8);String str ="-6156";// It will return a Integer instance// representing the specified stringSystem.out.println("Output Value = "+obj.valueOf(str));}}Output:Output Value = -6156
- The java.lang.Integer.valueOf(String s, int radix) is an inbuilt method which returns an Integer object, holding the value extracted from the specified String when parsed with the base given by the second argument.
Syntax :
public static Integer valueOf(String str, int base)
Parameter: The method accepts two parameters:
- str: This is of String type which is to be parsed.
- base This is of Integer type and refers to the base to be used to interpret str.
Return Value : The method returns an Integer object holding the value represented by the string argument in the specified base or radix.
Examples:
Input: str = "1101" base = 2 Output: 13 Input: str = "101" base = 4 Output: 17Below program illustrates the java.lang.Integer.valueOf(String str, int base) method:
// Java praogram to illustrate the// java.lang.Integer.valueOf(String str, int base)importjava.lang.*;publicclassGeeks {publicstaticvoidmain(String[] args){// Base = 2Integer val1 = Integer.valueOf("1010",8);System.out.println(val1);// Base = 16Integer val2 = Integer.valueOf("1011",16);System.out.println(val2);// Base = 2Integer val3 = Integer.valueOf("1010",2);System.out.println(val3);// Base = 10Integer val4 = Integer.valueOf("1021",10);System.out.println(val4);}}Output:520 4113 10 1021
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


