Autoboxing and Unboxing in Java
Fast recall
Quick revision
Core answer
int, double, etc.) to its wrapper class (Integer, Double, etc.) — you write Integer x = 5 and Java silently calls Integer.valueOf(5). Unboxing is the reverse — when you use a wrapper object where a primitive is expected, Java calls .intValue() automatically.Whether you understand what autoboxing/unboxing actually is under the hood — compiler-generated calls to valueOf() and intValue() — and where it creates silent overhead.
Thinking autoboxing is free. Every box creates a new object on the heap (except for the Integer cache -128 to 127). In tight loops this adds up.
Mention that autoboxing can throw a NullPointerException during unboxing — if an Integer variable is null and Java tries to unbox it to int, it crashes. This is a subtle and common real-world bug.
Interview-ready guide
Interview answer
The thing to get across is that none of this is free, even though it looks invisible. Every time you write Integer x = 5, the compiler quietly rewrites it to Integer.valueOf(5), and every time you use that wrapper as a number — say int y = x — it inserts x.intValue(). So boxing and unboxing are real method calls and real heap allocations hiding behind ordinary-looking syntax.
Where it actually shows up is collections. You can't write ArrayList, only ArrayList, so every list.add(42) boxes a primitive into a new object. In a tight loop doing millions of operations, that's millions of throwaway objects and real GC pressure — which is why I'd reach for an int[] or an IntStream when performance matters.
The detail I'd be sure to mention is the failure mode: unboxing a null Integer throws a NullPointerException. A line like int total = someInteger becomes someInteger.intValue(), and if that wrapper is null you crash — on what reads like a plain assignment with no method call in sight.
So my take: autoboxing is a great convenience for readability, but I treat any int-to-Integer boundary as two things to watch — an allocation and a possible null.
Autoboxing and Unboxing in Java
Direct Answer
Autoboxing is the automatic conversion Java does when you assign a primitive (int, double, etc.) to its wrapper class (Integer, Double, etc.) — you write Integer x = 5 and Java silently calls Integer.valueOf(5). Unboxing is the reverse — when you use a wrapper object where a primitive is expected, Java calls .intValue() automatically.
Code Example
import java.util.ArrayList;
public class AutoboxDemo {
public static void main(String[] args) {
// Autoboxing — int to Integer
Integer x = 5; // compiler: Integer.valueOf(5)
Integer y = 100; // compiler: Integer.valueOf(100)
// Unboxing — Integer to int
int sum = x + y; // compiler: x.intValue() + y.intValue()
System.out.println(sum); // 105
// Collections require objects — autoboxing happens on every add
ArrayList<Integer> list = new ArrayList<>();
list.add(42); // autoboxed: Integer.valueOf(42)
list.add(99); // autoboxed: Integer.valueOf(99)
int first = list.get(0); // unboxed: .intValue()
// NullPointerException trap during unboxing
Integer n = null;
// int value = n; // NullPointerException — cannot unbox null
// n.intValue() is called implicitly — null has no .intValue()
// Safe unboxing — always null-check first
if (n != null) {
int value = n; // safe
}
}
}
Common Mistakes & Pitfalls
- Assuming autoboxing is free — every box is a
newheap object (outside the-128to127cache); a loop that boxes a million times creates a million objects and real GC churn. - Unboxing a
nullwithout thinking —int y = someIntegercompiles fine but throwsNullPointerExceptionat runtime if the wrapper isnull; the crash hides inside the insertedintValue()call. - Mixing
intandIntegerin hot paths — usingArrayList<Integer>for heavy number-crunching boxes on everyadd()and unboxes on everyget(); switch toint[]orIntStreamwhen throughput matters. - Comparing wrappers with
==—==checks object identity, not value, so it works only by luck inside the cache range; use.equals()forIntegercomparisons.
Check your understanding
Answer these without looking back at the article.
- How can autoboxing cause a NullPointerException in Java?
- What is the Integer cache in Java and why does it affect == comparison?
- Why can't you use primitive types as generic type parameters in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.