What is autoboxing and unboxing in Java?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Java development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
Autoboxing is the automatic conversion that Java performs from a primitive type to its corresponding wrapper class. For example, assigning an int to an Integer variable: Integer x = 5; — the compiler automatically wraps 5 in an Integer object. Unboxing is the reverse: automatic conversion from a wrapper class to its primitive. For example: int y = x;. While convenient, autoboxing can cause performance issues and NullPointerExceptions if you accidentally unbox a null Integer. Be mindful of autoboxing in tight loops or large collections.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong Java candidates.
Previous
What is the difference between int and Integer in Java?
Next
What is the difference between == and .equals() in Java?