Sub topic 6
The Wrapper Cache and the `==` Surprise
Assume you know Java primitive types, object references, ==, equals, and generic collections. This chapter focuses on why two Integer objects can sometimes compare equal with ==, and why that result is unsafe to depend on.
What the wrapper cache does
An Integer is an object that stores an int value. Java can reuse some Integer objects instead of creating a new object every time. This reuse is called caching.
Integer.valueOf returns a cached object for values from -128 through 127. Java requires this range to be cached. An implementation may cache additional values, but code must not assume that it does.
public class IntegerCacheDemo {
public static void main(String[] args) {
Integer smallA = Integer.valueOf(127);
Integer smallB = Integer.valueOf(127);
Integer largeA = Integer.valueOf(1000);
Integer largeB = Integer.valueOf(1000);
System.out.println(smallA == smallB);
System.out.println(largeA == largeB);
}
}Typical output is:
true
falseBoth variables in each pair refer to objects with the same numeric value. However, == compares object references when both operands are wrappers. It asks whether the variables refer to the same object, not whether the objects contain equal values.
For 127, valueOf returns the same cached object. For 1000, it usually creates separate objects. The second comparison therefore usually prints false.
The exact result for values outside the required cache range can depend on the Java implementation and its configuration. That makes == especially dangerous for wrappers.
Autoboxing uses the factory
Autoboxing converts a primitive value into its wrapper automatically. For example:
public class BoxingCacheDemo {
public static void main(String[] args) {
Integer first = 127;
Integer second = 127;
Integer third = 1000;
Integer fourth = 1000;
System.out.println(first == second);
System.out.println(third == fourth);
}
}The compiler treats these assignments approximately as if they used Integer.valueOf:
Integer first = Integer.valueOf(127);That is why autoboxing can expose the cache. The result is still not a reason to use == for wrapper values.
| Comparison | What it checks | Recommended use |
|---|---|---|
| `a == b` | Whether two references identify the same object | Identity checks only |
| `a.equals(b)` | Whether two non-null wrappers have equal values | Value comparison |
| `Integer.compare(a, b) == 0` | Numeric ordering result | Value comparison with ordering |
Correct comparisons
Use equals when both references are known to be non-null:
public class WrapperComparison {
public static void main(String[] args) {
Integer left = 1000;
Integer right = 1000;
System.out.println(left.equals(right)); // true
System.out.println(left.compareTo(right) == 0); // true
}
}If either wrapper might be null, calling equals on that wrapper can throw NullPointerException. Objects.equals handles two nullable references safely.
import java.util.Objects;
public class NullableComparison {
public static void main(String[] args) {
Integer left = null;
Integer right = 10;
System.out.println(Objects.equals(left, right)); // false
System.out.println(Objects.equals(left, null)); // true
}
}Constructors and cache behavior
Older code may use a constructor:
Integer value = new Integer(127);That constructor creates a distinct object and is deprecated. It does not use the shared cache. Prefer Integer.valueOf(127) or ordinary autoboxing:
Integer value = Integer.valueOf(127);
Integer sameStyle = 127;The factory communicates the intended conversion and permits cache reuse.
Exercise
Why can Integer a = 127; Integer b = 127; a == b be true, while the same comparison with 1000 is usually false? Which comparison should production code use?
Show answer
Autoboxing calls Integer.valueOf, which reuses the required cache for -128 through 127. Larger values usually produce separate objects, so == compares different references. Use equals, Objects.equals, or compareTo for wrapper values.