Integer valueOf() Method in Java
-
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));}}chevron_rightfilter_noneOutput: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));}}chevron_rightfilter_noneOutput: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));}}chevron_rightfilter_noneOutput: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));}}chevron_rightfilter_noneOutput: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);}}chevron_rightfilter_noneOutput:520 4113 10 1021
Recommended Posts:
- Integer.valueOf() vs Integer.parseInt() with Examples
- BigInteger valueOf() Method in Java
- BigDecimal valueOf() Method in Java
- DayOfWeek valueOf() method in Java with Examples
- Data Conversion Using valueOf() method in Java
- TimeUnit valueOf() method in Java with Examples
- Locale.FilteringMode valueOf() method in Java with Examples
- Locale.Category valueOf() method in Java with Examples
- Java.lang.Long.valueOf() method with examples
- Character.valueOf() in Java with examples
- Integer sum() Method in Java
- Java Integer compare() method
- Integer rotateRight() Method in Java
- Integer shortValue() Method in Java
- Java Integer bitCount() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



