Interview Q&A · Java · 5 questions in this topic
Wrapper Classes and Autoboxing — Java Interview Q&A
Object forms of primitives, their mappings, and how autoboxing converts between the two. Part of Objects, References & Memory · 294 questions in the Java bank.
What are wrapper classes?
Answer
Wrapper classes are immutable object versions of the eight primitives, needed wherever an Object is required such as collections and generics. Since Java 5 the compiler converts between primitives and wrappers automatically through autoboxing and unboxing. You should still prefer primitives in arithmetic and hot loops to avoid hidden allocation and null unboxing failures.
Key points
- 1Wrappers are immutable: once created, the wrapped value never changes.
- 2Needed for generics and collections, which cannot hold primitives directly.
- 3Unboxing a null wrapper throws NullPointerException.
- 4Autoboxing can allocate or reuse cached objects inside loops, so keep primitives in arithmetic-heavy code.
- 5Integer.valueOf returns a cached wrapper, while Integer.parseInt returns a primitive int.
Example
List<Integer> scores = new ArrayList<>(); scores.add(42); // autoboxing: int 42 becomes Integer.valueOf(42) int first = scores.get(0); // unboxing back to int
List the primitive-to-wrapper mappings.
Answer
Each primitive has one wrapper: boolean maps to Boolean, byte to Byte, short to Short, int to Integer, long to Long, float to Float, double to Double, and char to Character. The wrappers cache small values, most visibly Integer values from -128 to 127, so identity comparison with == can mislead and equals should be used instead.
Key points
- 1boolean-Boolean, byte-Byte, short-Short, int-Integer.
- 2long-Long, float-Float, double-Double, char-Character.
- 3Use equals for value comparison; == on wrappers compares identity.
What is autoboxing, and what are its pitfalls?
Answer
Autoboxing converts a primitive to its wrapper automatically — int to Integer — and unboxing does the reverse. The usual traps: unboxing a null wrapper throws NullPointerException; == on two wrappers compares references, and only values in the Integer cache (-128 to 127) are guaranteed to be the same object; and boxing inside a hot loop allocates a new object per iteration, which can dominate the profile. Use equals for value comparison and prefer primitives in performance-sensitive code.
Key points
- 1Autoboxing is compiler sugar, not a free conversion.
- 2== compares wrapper references; equals compares values.
- 3Integer.valueOf caches -128 to 127 by default.
- 4Unboxing null throws NullPointerException.
Example
Integer a = 127, b = 127; a == b; // true — cached Integer c = 128, d = 128; c == d; // false — separate objects
Why are wrapper objects needed at all if primitives are faster?
Answer
Generics and collections cannot hold primitives: List<int> is illegal, so List<Integer> is the idiom, and the compiler boxes on the way in and out. Wrappers also carry a null state that primitives cannot express, which matters for optional database columns and JSON fields, and they provide utility methods such as Integer.parseInt, Integer.toBinaryString and Double.isNaN. Primitives remain the right choice for arithmetic and hot loops.
Key points
- 1Collections and generics require reference types.
- 2A wrapper can be null; a primitive cannot.
- 3Wrappers provide parsing and conversion helpers.
- 4Use primitives where null is not meaningful.
What is the difference between parseInt and valueOf?
Answer
Integer.parseInt(String) returns a primitive int and allocates nothing beyond the parsing work. Integer.valueOf(String) returns an Integer, reusing the cached instances for -128 to 127 and allocating outside that range. Autoboxing calls valueOf implicitly, which is why assigning a parsed number to an Integer can reuse a cached object. Prefer parseInt when only the primitive is needed, and valueOf only when a boxed value is genuinely required.
Key points
- 1parseInt returns int; valueOf returns Integer.
- 2valueOf uses the integer cache; parseInt does not allocate.
- 3Autoboxing compiles to a valueOf call.
- 4Both throw NumberFormatException on malformed input.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission