Explain autoboxing and unboxing in Java
π‘ Concept: Autoboxing & Unboxing
Autoboxing is the automatic conversion of primitive types to their corresponding wrapper classes. Unboxing is the reverseβconverting wrapper objects back to primitives.
π Quick Intro
Before Java 5, conversions between int
and Integer
(and other pairs) had to be done manually. Java 5+ introduced autoboxing and unboxing to reduce boilerplate.
π§ Analogy
Imagine putting a coin (primitive) inside a piggy bank (wrapper object)βthatβs autoboxing. Taking it back out to spend itβthatβs unboxing.
π§ Technical Explanation
- Autoboxing:
int
βInteger
,char
βCharacter
, etc. - Unboxing:
Integer
βint
,Double
βdouble
, etc. - Happens automatically in assignments, method calls, or collections.
- Boxed types can be
null
βbeware ofNullPointerException
during unboxing. - Useful for generics which only support objects.
π― Use Cases
- β
Using primitives in collections like
List<Integer>
- β Passing primitives to methods expecting wrappers
- β Interchanging primitives and wrappers with minimal effort
π» Example: Autoboxing and Unboxing
import java.util.ArrayList;
public class BoxingDemo {
public static void main(String[] args) {
int num = 10;
// Autoboxing: int β Integer
Integer boxed = num;
// Unboxing: Integer β int
int unboxed = boxed;
// Using with collections
ArrayList<Integer> list = new ArrayList<>();
list.add(num); // Autoboxing
int x = list.get(0); // Unboxing
System.out.println("Boxed: " + boxed + ", Unboxed: " + unboxed);
}
}

β Interview Q&A
Q1: What is autoboxing?
A: Automatic conversion from a primitive to a wrapper object.
Q2: What is unboxing?
A: Conversion from a wrapper to a primitive.
Q3: When was it introduced?
A: Java 5.
Q4: Can unboxing throw exceptions?
A: Yes, NullPointerException
if the wrapper is null.
Q5: Why is autoboxing useful with collections?
A: Collections like List<>
only support objects.
Q6: Is boxing explicit or implicit?
A: Implicit (automatic).
Q7: Can you disable autoboxing?
A: No, itβs part of the language.
Q8: Is Integer
immutable?
A: Yes.
Q9: What is the wrapper class for double
?
A: Double
.
Q10: Can you compare primitives and wrappers directly?
A: Yes, due to unboxing.
π MCQs
Q1. What is autoboxing?
- Object to primitive
- Primitive to wrapper
- String to int
- Class to object
Q2. What is unboxing?
- Primitive to wrapper
- Wrapper to primitive
- Method to value
- Loop conversion
Q3. Which version introduced autoboxing?
- Java 8
- Java 1.3
- Java 5
- Java 11
Q4. Can unboxing cause exception?
- No
- Yes, if null
- Only in Java 6
- Never
Q5. Which is wrapper for int?
- IntObj
- Integer
- IntWrapper
- WholeNumber
Q6. What is stored in List<Integer>?
- Primitives
- Strings
- Wrapper objects
- Booleans only
Q7. Is boxing done manually?
- Yes
- No, automatic
- Optional
- With import
Q8. Can wrapper be null?
- No
- Yes
- Only in arrays
- Only static
Q9. What happens on unboxing null?
- 0 returned
- Type error
- NullPointerException
- Infinity
Q10. Is Double the wrapper for double?
- No
- Yes
- doubleWrapper
- DType
π‘ Bonus Insight
While autoboxing simplifies syntax, excessive boxing/unboxing can affect performance in loops. Prefer primitives in performance-critical code.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!