Interview Question

What is auto-boxing and unboxing?

Autoboxing and unboxing convert between primitives and wrapper objects.

💡 Concept ✅ Quick Revision ☕ Java

Answer

Boxing converts a primitive value to its wrapper type; unboxing extracts a primitive from a wrapper. • The compiler can insert these conversions automatically in supported contexts. • Unboxing null throws NullPointerException. • Wrapper identity should not be used as numeric value equality.

Example

Code
Integer boxed = 42;
int value = boxed;
System.out.println(value + 1);
Output
43

Quick Revision

Autoboxing and unboxing convert between primitives and wrapper objects.