← Back to Java

Wrapper Classes

JavaEntryjava

The Question

What are wrapper classes in Java?

What a Strong Answer Covers

  • convert primitives to objects
  • Integer/Double/Boolean etc.
  • autoboxing = primitive to wrapper
  • unboxing = wrapper to primitive
  • needed for collections/APIs

Senior-Level Answer

Java has eight primitive types — `byte`, `short`, `int`, `long`, `float`, `double`, `char`, and `boolean` — that are not objects and cannot be used where `Object` references are required. Wrapper classes provide an object counterpart for each: `Byte`, `Short`, `Integer`, `Long`, `Float`, `Double`, `Character`, and `Boolean`.

The primary motivation is interoperability with the Java Collections Framework. `ArrayList<int>` is illegal — generics require reference types. You write `ArrayList<Integer>` instead. Wrapper classes bridge the gap.

Java 5 introduced autoboxing and unboxing to reduce boilerplate. Autoboxing is the automatic conversion from primitive to wrapper (`int` → `Integer`); unboxing is the reverse. The compiler inserts the conversion calls transparently, so `Integer i = 5` compiles to `Integer.valueOf(5)`, and `int x = i` compiles to `i.intValue()`.

Wrapper classes also carry utility methods. `Integer.parseInt("42")` converts strings to primitives. `Integer.MAX_VALUE`, `Integer.MIN_VALUE`, `Integer.toBinaryString()` are all available on the class. This makes them useful beyond mere boxing.

A critical performance and correctness caveat: `Integer` (and other integer wrappers) caches instances for values from -128 to 127. `Integer.valueOf(100) == Integer.valueOf(100)` is `true` because both return the cached instance. `Integer.valueOf(200) == Integer.valueOf(200)` is `false` — new objects. This is a common source of subtle bugs when using `==` instead of `equals()` with wrapper types.

Another pitfall is NullPointerException during unboxing. If an `Integer` field is null and you assign it to an `int`, the JVM calls `.intValue()` on null and throws NPE. This happens invisibly because the unboxing is compiler-generated.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains the purpose of wrapping primitives for use in collections/generics and describes autoboxing/unboxing.

3/3 — Strong Answer

Also covers the Integer cache (-128 to 127), the == vs equals() trap, utility methods on wrapper classes, and NPE risk from unboxing null.

Common Mistakes

  • Using == to compare wrapper objects and being surprised by inconsistent results due to the integer cache range.
  • Not considering NullPointerException risk when unboxing a wrapper that may be null.
  • Thinking autoboxing is free — repeated boxing in tight loops creates significant garbage collection pressure.

Follow-Up Questions

  • Why does `Integer a = 127; Integer b = 127; a == b` return true, but the same code with 128 returns false? — Integer.valueOf() uses a cache for -128 to 127; outside that range, new objects are allocated.
  • What are the performance implications of autoboxing inside a tight loop? — Each autobox allocates a new object; heavy loops can generate significant GC pressure. Use primitives or primitive collections.
  • How would you safely unbox an Integer that might be null? — Null-check before unboxing, or use a default with Objects.requireNonNullElse() or Optional.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout