What is Autoboxing and Unboxing in Java?
Learn what autoboxing and unboxing in Java are, how primitives convert to wrapper classes, the null unboxing pitfall, == caveats and examples.
Expected Interview Answer
Autoboxing is the automatic conversion the Java compiler makes from a primitive type (like int) to its corresponding wrapper class (like Integer), and unboxing is the reverse conversion from a wrapper object back to a primitive.
Introduced in Java 5, these conversions let primitives and wrapper objects be used interchangeably, which is essential because generic collections such as List<Integer> can only hold objects, not primitives. Autoboxing calls Integer.valueOf() under the hood while unboxing calls intValue(). The convenience carries costs: unboxing a null wrapper throws NullPointerException, boxing in loops creates extra objects, and == on wrappers compares references rather than values.
- Lets primitives work with generic collections
- Removes manual valueOf()/intValue() calls
- Cleaner, more readable arithmetic with wrappers
- Interoperability between primitives and objects
- Enables primitives in APIs that expect Object
AI Mentor Explanation
Autoboxing is like putting a bare cricket ball into a labeled match box so it can go into the storeroom's catalogued shelf, which only accepts boxed items. Unboxing is taking the ball back out to actually bowl with it. The ball is the primitive int; the box is the Integer wrapper the collection shelf demands.
Step-by-Step Explanation
Step 1
Write a primitive assignment
Assign a primitive to a wrapper reference, e.g. Integer i = 5;
Step 2
Compiler autoboxes
The compiler inserts Integer.valueOf(5), converting the primitive to a wrapper object.
Step 3
Store in a collection
Add primitives to List<Integer> or Map values; each is autoboxed on insertion.
Step 4
Read back a value
Assign a wrapper to a primitive, e.g. int x = i; the compiler inserts i.intValue().
Step 5
Guard against null
Check for null before unboxing so a null wrapper does not throw NullPointerException.
What Interviewer Expects
- Correct definition of autoboxing and unboxing
- The primitive-to-wrapper mapping (int to Integer, etc.)
- Awareness it was added in Java 5
- Knowledge that unboxing null throws NullPointerException
- Understanding of == vs equals on wrapper objects
Common Mistakes
- Comparing wrappers with == expecting value equality
- Unboxing a null Integer and hitting NullPointerException
- Ignoring performance cost of boxing inside tight loops
- Confusing which is boxing and which is unboxing
- Assuming Integer caching (-128 to 127) applies to all values
Best Answer (HR Friendly)
“Autoboxing is when Java automatically wraps a simple value like a number into its object form, and unboxing is turning that object back into a simple value. It happens behind the scenes so numbers can be stored in lists and other collections that only accept objects.”
Code Example
import java.util.ArrayList;
import java.util.List;
public class BoxingDemo {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
nums.add(10); // autoboxing: int 10 -> Integer.valueOf(10)
int first = nums.get(0); // unboxing: Integer -> intValue()
System.out.println(first + 5); // 15
Integer maybe = null;
// int bad = maybe; // would throw NullPointerException on unboxing
System.out.println(maybe == null); // safe null check
}
}Follow-up Questions
- Why can primitives not be stored directly in a List?
- What exception occurs when you unbox a null wrapper?
- Why is == unreliable for comparing Integer objects?
- How does the Integer cache (-128 to 127) affect ==?
- What is the performance impact of autoboxing in loops?
MCQ Practice
1. What does autoboxing convert?
Autoboxing converts a primitive (like int) to its wrapper object (like Integer); the reverse is unboxing.
2. What happens when you unbox a null Integer to an int?
Unboxing calls intValue() on the wrapper; if it is null, a NullPointerException is thrown at runtime.
3. Which method does autoboxing of an int call internally?
The compiler inserts Integer.valueOf(x) for autoboxing, which also uses the Integer cache for -128..127.
Flash Cards
What is autoboxing? — Automatic conversion of a primitive to its wrapper class, e.g. int to Integer via Integer.valueOf().
What is unboxing? — Automatic conversion of a wrapper object back to a primitive, e.g. Integer to int via intValue().
What risk comes with unboxing? — Unboxing a null wrapper throws NullPointerException at runtime.
When was autoboxing introduced? — In Java 5, alongside generics and enhanced for-loops.