int vs Integer — Primitive vs Wrapper and Autoboxing
Fast recall
Quick revision
Core answer
int is a 32-bit primitive value and cannot be null. Integer is an immutable wrapper object and can be null; it is needed by generics and APIs that use reference types. Java can autobox and unbox between them, but unboxing null throws NullPointerException, and wrapper identity must not be compared with == as a value test.Whether the candidate understands the primitive/object divide and why wrappers exist for generics and collections
Forgetting that unboxing a null Integer throws a NullPointerException, and using == instead of .equals() to compare Integer objects
Explain that List<int> is illegal so wrappers exist for generics, and mention the Integer cache (-128..127) as the reason == sometimes works and sometimes doesn't
Interview-ready guide
Interview answer
The cleanest way to put it is that `int` is the raw value and `Integer` is an object that wraps that value. int is a primitive value, holds the number directly, and can never be null; Integer lives on the heap, can be null, and carries helper methods. The reason the wrapper exists at all is generics — collections only hold objects, so you can't write List, you write List. To make that painless, the compiler autoboxes an int into an Integer and unboxes it back automatically, inserting valueOf and intValue for you.
The detail I'd make sure to land is the null trap: unboxing a null Integer throws a NullPointerException at runtime, because that hidden intValue() call runs on null — and it compiles fine, so nothing warns you. I'd also note you compare Integers with .equals(), not ==, since == only seems to work for small cached values. So my rule is: default to `int`, and reach for Integer only when you genuinely need null or generics.
int vs Integer — Primitive vs Wrapper and Autoboxing
Direct Answer
int is a 32-bit primitive value and cannot be null. Integer is an immutable wrapper object and can be null; it is needed by generics and APIs that use reference types. Java can autobox and unbox between them, but unboxing null throws NullPointerException, and wrapper identity must not be compared with == as a value test.
Code Example
This is the everyday reason wrappers and autoboxing exist: collections can only hold objects, so the int literals you add are boxed on the way in and unboxed on the way out — all without you writing a single conversion.
import java.util.*;
public class BoxingDemo {
public static void main(String[] args) {
List<Integer> scores = new ArrayList<>();
scores.add(95); // autobox: int 95 -> Integer.valueOf(95)
scores.add(80); // autobox again
int first = scores.get(0); // unbox: Integer -> int
int sum = scores.get(0) + scores.get(1); // unbox both, then add
System.out.println("First: " + first + ", Sum: " + sum);
}
}
// Output:
// First: 95, Sum: 175
Notice List<Integer> — never List<int>. Every add(95) boxes the primitive into an object, and every get unboxes it back. Convenient, but in a tight loop that hidden boxing allocates a fresh object each time, which is why performance-sensitive code prefers a primitive int[] over a List<Integer>.
Common Mistakes & Pitfalls
- Unboxing a
nullwrapper:Integer x = map.get("missing"); int y = x;throws aNullPointerExceptionbecausegetreturnednulland unboxing calledintValue()on it. Always treat a wrapper from a map, query, or API as possiblynull. - Comparing
Integerwith==:==compares object references, not values. It appears to work for small numbers because of theIntegercache (−128 to 127 are shared objects), then fails for1000 == 1000. Use.equals()or unbox tointfirst. - Using
Integereverywhere by habit: a wrapper allocates an object and can benull, so prefer plainintfor counters, loop variables, and fields that always have a value — reach forIntegeronly when you genuinely neednullor generics. - Boxing in hot loops:
Integer sum = 0; for (...) sum += x;re-boxes on every iteration, creating thousands of throwaway objects. Use anintaccumulator instead.
Check your understanding
Answer these without looking back at the article.
- What is the Integer cache and why does == sometimes work for Integer?
- What happens when you unbox a null Integer?
- Why can't you write List<int> in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.