CKTIS / Java
Integer.valueOf: explicitly boxing an int
Turn a primitive `int` into an `Integer`, distinguish boxing from parsing text, and see why wrapper identity is not a reliable test of numeric equality.
Version: Java SE 21
Syntax
public static Integer valueOf(int i)
public static Integer valueOf(String s) throws NumberFormatException
public static Integer valueOf(String s, int radix) throws NumberFormatExceptionInteger.valueOf is the name of an overloaded static method on java.lang.Integer in Java SE 21. Integer.valueOf(int) returns an Integer representing the supplied primitive value; it does not parse text. Integer.valueOf(42) boxes explicitly, while Integer n = 42; uses autoboxing. Assigning the result to an Object variable does not change the actual object type: it is still an Integer.
The overloads Integer.valueOf(String) and Integer.valueOf(String, int) parse a signed integer first, using base 10 or the specified radix, respectively. Malformed text, null, a value outside the int range, or an invalid radix causes NumberFormatException. If you need an int result, use the corresponding Integer.parseInt overload.
Integer.valueOf(int) guarantees shared instances for values from -128 through 127, inclusive. It may reuse instances outside that range, but do not rely on it. With two Integer references, == checks identity; use equals to compare the values of non-null wrappers. With an Integer and an int, == unboxes the wrapper and compares the numbers. Unboxing null throws NullPointerException.
In a nested map, word.length() returns an int. Calling Integer.valueOf(word.length()) explicitly boxes that length, so each group can be collected as a List<Integer>.
Examples
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Object boxed = Integer.valueOf(42);
System.out.println(boxed);
System.out.println(boxed == Integer.valueOf(42));
List<List<Integer>> lengths = Stream.of(List.of("Java", "SE"), List.of("API"))
.map(group -> group.stream()
.map(word -> Integer.valueOf(word.length()))
.toList())
.toList();
System.out.println(lengths.get(0).get(0) + ", " + lengths.get(0).get(1));
System.out.println(lengths.get(1).get(0));
}
}Result
42 true 4, 2 3
The `Object` variable `boxed` holds a reference to an `Integer` representing `42`. The comparison prints `true` because both calls return the same cached instance for `42`. The inner `map` turns words into boxed lengths: `4, 2` in the first group and `3` in the second.
Related lessons
- Java · Lesson 9 · Advanced · Version: 2
- Java · Lesson 44 · Intermediate · Version: 2
Sources
- Java SE 21 API — Integer.valueOf(int)
- Java SE 21 API — Integer.valueOf(String)
- Java SE 21 API — Integer.valueOf(String, int)
- Java SE 21 API — Integer.equals(Object)
- Java Language Specification, Java SE 21 — §5.1.7 Boxing Conversion and §5.1.8 Unboxing Conversion
- Java Language Specification, Java SE 21 — §15.21 Equality Operators
- Java SE 21 API — Stream.map(Function)
- Java SE 21 API — Stream.toList()