Lesson · Java Language Fundamentals
Wrappers and autoboxing: the cache that decides ==
12 min readWrapper Classes & Autoboxing
Wrapper classes turn primitives into objects so generics and collections can hold them. Autoboxing converts silently in both directions, and one small cache makes == answer differently at 127 and at 128.
Why wrappers exist
Generic type parameters cannot be primitives, and neither can collections: there is no List<int>, only List<Integer>. Every primitive therefore has an object twin (Byte, Short, Integer, Long, Float, Double, Character, Boolean) whose whole job is to be a box for one value. Wrappers are immutable, which is what makes sharing them safe.
The cost is layout. A wrapper object carries an object header plus the value, tens of bytes where the primitive would need four or eight. It can also be null, which is simultaneously the feature (absence is representable) and the hazard (unboxing null throws).
Autoboxing in both directions
Boxing is the compiler calling Integer.valueOf(5) where you wrote 5 in an object context. Unboxing is the compiler calling intValue() where you wrote a wrapper in a numeric context. Both are invisible in source, and both have moments they trigger: assignment, method arguments, arithmetic that mixes a wrapper with a primitive, and == between the two kinds.
Performance follows the call sites, not the syntax. A loop that adds into a List<Integer> boxes on every add, allocates on most of them, and grows the left-hand type to long first if the sum can pass two billion. In hot code, that is measurable garbage creation; in ordinary application code, it is fine and the readability is worth it.
Where each Integer points
Integer cache · -128 to 127
Heap
nothing allocated yet
References
no variables yet
Integer.valueOf does not always allocate. Watch where each variable points: a shared cache, or fresh heap objects.
The cache, and why == lies
Integer.valueOf allocates for most values, but returns a shared cached instance for anything from -128 to 127. The other wrappers cache the same range (Character from 0 to 127). So Integer a = 127; Integer b = 127; produces two variables pointing at one object, and a == b is true. Change 127 to 128 and the cache misses twice: two equal objects, two references, == false, equals true.
The rule that ends the confusion: never compare wrappers with ==. Use equals for content, or unbox explicitly with intValue() when you want primitive comparison and accept the NPE risk on null. The cache is an implementation detail, not a contract, and the cache upper bound can even be raised with a JVM flag; code that relies on it is a latent bug.
Null, overloads, and the practical rules
Unboxing null throws NullPointerException, which is where most wrapper pain surfaces: reading a Map<String, Integer> and adding one to the result fails on the first missing key; a Boolean field used directly in an if fails when unset; a ternary that mixes Integer and int unboxes the Integer side. Each is fixed the same way: check, default, or unbox deliberately.
One more wrinkle lives in overload resolution. A method call that could match either f(int) or f(Integer) chooses the primitive in phase one; only if no primitive fits does the compiler box and try again. That ordering is why list.remove(1) removes by index while list.remove(Integer.valueOf(1)) removes by value, the trap from the collections chapter. Wrappers are not evil; they are primitives wearing a suit, and the suit changes which door you go through.
a == c unboxes; a == b does not
With Integer a and b, a == b compares references and fails outside the cache. Add an int c and a == c unboxes a and compares numbers, so it passes. Same operator, three types, two very different meanings. Pick one rule and keep it: wrappers are compared with equals, primitives with ==.
Interview angles
Why does 127 == 127 hold for Integers but 128 == 128 fail?+
Integer.valueOf caches instances from -128 to 127 and returns the shared object inside that range. 127 gets the cached instance twice; 128 allocates two objects. The comparison is reference identity, not value equality, so the cache decides the answer. equals fixes it.
When does autoboxing happen, and what does it cost?+
On assignment, method arguments, arithmetic mixing wrappers and primitives, and == across the two kinds. Cost is allocation plus indirection; in hot loops and large collections that is real garbage, which is why int arrays or streams over primitives exist. Ordinary business code does not need to care.
Where does wrapper null blow up?+
Any unboxing: arithmetic on an Integer from a map miss, a Boolean in a condition, a ternary mixing Integer and int. The NPE arrives where the value is used, not where it was null. Default early with getOrDefault, or null-check before unboxing.
How does boxing affect method overload resolution?+
The compiler tries primitive conversions first and only boxes in a later phase, so an int argument prefers f(int) over f(Integer). This ordering explains why list.remove(1) picks remove(int index) rather than remove(Object value).