Integer valueOf() Method in Java (original) (raw)
The Integer.valueOf() method of the java.lang.Integer class is used to convert primitive values or strings into Integer objects. It is commonly preferred over constructors because it supports caching and better performance.
Why use valueOf() instead of new Integer()?
- valueOf() may return cached Integer objects (for values between -128 and 127).
- Avoids unnecessary object creation.
- Recommended approach since constructors like new Integer() are deprecated.
Method Variants of Integer.valueOf()
1. Integer.valueOf(int a)
Converts a primitive int value into its corresponding Integer wrapper object.
Syntax
public static Integer valueOf(_int a)
- **Parameters : a - primitive integer value
- **Return Value : Integer object representing the given int
**Example 1: valueOf(int) with Positive Number
Java `
public class Geeks { public static void main(String[] args) {
Integer value = Integer.valueOf(85);
System.out.println("Output Value = " + value);
}}
`
**Explanation:
- **valueOf(85) converts the primitive int to an Integer.
- JVM may reuse a cached object.
- Output is the same numeric value.
**Example 2: valueOf(int) with Negative Number
Java `
public class Geeks { public static void main(String[] args) {
Integer value = Integer.valueOf(-9185);
System.out.println("Output Value = " + value);
}}
`
Output
Output Value = -9185
**Explanation:
- Negative integers are also supported.
- A new or cached Integer object is returned.
- Value remains unchanged.
2: Integer.valueOf(String str)
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.
**Example 1: valueOf(String) with Positive Number
java `
public class Geeks { public static void main(String[] args) {
String str = "424";
Integer value = Integer.valueOf(str);
System.out.println("Integer Value = " + value);
}}
`
Output
Integer Value = 424
**Explanation:
- String "424" is parsed internally.
- Returned value is an Integer object.
- Parsing happens in base 10 by default.
**Example 2: valueOf(String) with Negative Number
Java `
public class Geeks { public static void main(String[] args) {
String str = "-6156";
Integer value = Integer.valueOf(str);
System.out.println("Output Value = " + value);
}}
`
Output
Output Value = -6156
**Explanation:
- Minus sign is correctly interpreted.
- String is converted into Integer.
- Returned object holds the parsed value.
3. Integer.valueOf(String str, int radix)
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 radix)
**Parameter: The method accepts two parameters:
- **str: This is of String type which is to be parsed.
- **radix : 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.
**Example 1: valueOf(String, radix)
Java `
public class Geeks { public static void main(String[] args) {
Integer val1 = Integer.valueOf("1010", 8);
Integer val2 = Integer.valueOf("1011", 16);
Integer val3 = Integer.valueOf("1010", 2);
Integer val4 = Integer.valueOf("1021", 10);
System.out.println(val1);
System.out.println(val2);
System.out.println(val3);
System.out.println(val4);
}}
`