Interview Q&A · Java · 5 questions in this topic

Wrapper Classes and Autoboxing — Java Interview Q&A

All Java topics

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

  1. 1Wrappers are immutable: once created, the wrapped value never changes.
  2. 2Needed for generics and collections, which cannot hold primitives directly.
  3. 3Unboxing a null wrapper throws NullPointerException.
  4. 4Autoboxing can allocate or reuse cached objects inside loops, so keep primitives in arithmetic-heavy code.
  5. 5Integer.valueOf returns a cached wrapper, while Integer.parseInt returns a primitive int.

Example

Example.javajava
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

  1. 1boolean-Boolean, byte-Byte, short-Short, int-Integer.
  2. 2long-Long, float-Float, double-Double, char-Character.
  3. 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

  1. 1Autoboxing is compiler sugar, not a free conversion.
  2. 2== compares wrapper references; equals compares values.
  3. 3Integer.valueOf caches -128 to 127 by default.
  4. 4Unboxing null throws NullPointerException.

Example

Example.javajava
Integer a = 127, b = 127;
a == b;        // true — cached
Integer c = 128, d = 128;
c == d;        // false — separate objects

Learn this topic

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits