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 of NullPointerException 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&lt;Integer&gt;?

  • 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!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: