← Back to Home

Autoboxing & Unboxing

Autoboxing and unboxing are Java features that automatically convert primitive data types to their wrapper objects and vice versa. Introduced in Java 5, they simplify code when working with collections, generics, and APIs.

What Is Autoboxing?

Autoboxing is the automatic conversion of a primitive type into its corresponding wrapper class.

int a = 10;
Integer b = a;   // autoboxing
          

Behind the scenes:

Integer b = Integer.valueOf(a);
          

What Is Unboxing?

Unboxing is the automatic conversion of a wrapper object into its corresponding primitive type.

Integer x = 20;
int y = x;   // unboxing
          

Behind the scenes:

int y = x.intValue();
          

Why Autoboxing & Unboxing Exist

  • Simplifies code
  • Enables primitives to work with collections and generics
  • Improves readability

Example (Collections)

List<Integer> list = new ArrayList<>();
list.add(5);      // autoboxing
int n = list.get(0); // unboxing
          

Wrapper Mapping (Quick Reference)

Primitive Wrapper
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Common Autoboxing Scenarios

  1. Assignment
    Integer i = 100;  // autoboxing
                  
  2. Method Arguments
    void print(Integer x) {}
    print(10); // autoboxing
                  
  3. Return Values
    Integer getValue() {
        return 20;  // autoboxing
    }
                  

Common Unboxing Scenarios

  1. Assignment
    Integer a = 50;
    int b = a;  // unboxing
                  
  2. Expressions
    Integer x = 10;
    Integer y = 20;
    int sum = x + y; // unboxing
                  

Important Interview Traps & Edge Cases

1. NullPointerException During Unboxing

Integer x = null;
int y = x;   // ❌ NullPointerException
          

Why it happens: Unboxing calls intValue() on a null object.

2. Performance Impact (Autoboxing in Loops)

for (Integer i = 0; i < 1000; i++) {
    // repeated boxing and unboxing
}
          

Best practice: Use primitives in loops.

3. Wrapper Object Comparison (== vs equals())

Integer a = 100;
Integer b = 100;
System.out.println(a == b);      // true (cached)
System.out.println(a.equals(b)); // true

Integer x = 200;
Integer y = 200;
System.out.println(x == y);      // false
          

Why: Integer caching range is -128 to 127.

Autoboxing with Arithmetic Operations

Integer a = 10;
Integer b = 20;
Integer c = a + b;  // unboxing + boxing
          

Behind the scenes:

  • Unbox a and b
  • Perform addition
  • Box result back to Integer

When to Use and When to Avoid

Use Autoboxing When:

  • Working with collections
  • Using generics
  • Interacting with frameworks

Avoid Autoboxing When:

  • Performance-critical code
  • Tight loops
  • Low-level calculations

Common Beginner Mistakes

  • Assuming wrapper objects never become null
  • Using == for wrapper comparison
  • Ignoring performance costs
  • Overusing wrappers instead of primitives

Interview-Ready Answers

Short Answer

Autoboxing is automatic conversion from primitive to wrapper, and unboxing is conversion from wrapper to primitive.

Detailed Answer

Autoboxing and unboxing allow Java to automatically convert between primitive types and their corresponding wrapper classes. This simplifies code when using collections and generics but can cause performance issues and NullPointerExceptions if not handled carefully.

Key Takeaway

Autoboxing and unboxing improve developer productivity, but understanding their internal behavior and pitfalls is essential for writing efficient and safe Java code.