Java Interview Question

What is autoboxing in Java?

Updated 2026-07-10 · Beginner friendly
Quick answer

Autoboxing is the automatic conversion of a primitive like int into its wrapper object like Integer, and unboxing is the reverse. Java does this for you so primitives can be used where objects are required, such as inside collections. It is convenient but can add hidden overhead in tight loops.

Why it exists

Collections like ArrayList can only hold objects, not primitives. Autoboxing lets you add an int to a List of Integer without writing the conversion yourself.

List<Integer> nums = new ArrayList<>();
nums.add(5);          // autoboxing: int 5 -> Integer
int first = nums.get(0);  // unboxing: Integer -> int

The gotcha

Autoboxing creates objects, so doing it millions of times in a loop can slow things down and use extra memory. It can also cause a surprise NullPointerException if you unbox a null Integer.

In the interview

Mention the null unboxing trap: unboxing a null Integer throws a NullPointerException. Pointing out both the convenience and the hidden cost shows you understand the feature beyond the surface.

Want the full Java guide?

Read every Java concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the Java guide All Java questions