01 / 20

Wrapper Classes
& Autoboxing

Learn how Java bridges primitive types and objects with wrapper classes, and how autoboxing and unboxing make this seamless.

AP Computer Science A Java

Why Do We Need Wrapper Classes?

Java has two type systems: primitives (like int, double, boolean) that hold raw values, and objects that live on the heap. Many Java features, like ArrayList, only work with objects: you can't write ArrayList<int>.

The Three Key Wrapper Classes

Integer
Wraps the primitive int. Stores a single 32-bit integer value as an object.
Double
Wraps the primitive double. Stores a 64-bit floating-point value as an object.
Boolean
Wraps the primitive boolean. Stores true or false as an object.

Primitive vs Object in Memory

Primitive (int) 42 Stack memory Direct value storage wrap Object (Integer) 42 Heap memory Object with methods int x = 42; Integer x = 42;

A primitive stores its value directly on the stack. A wrapper object lives on the heap and contains the value inside, plus it inherits methods from Object.

What Is Autoboxing?

// You write: Integer num = 42; // Compiler converts to: Integer num = Integer.valueOf(42);

The literal 42 is a primitive int. Java automatically calls Integer.valueOf(42) behind the scenes to create the wrapper object.

What Is Unboxing?

// You write: Integer obj = 100; int val = obj; // unboxing // Compiler converts to: int val = obj.intValue();

When you assign a wrapper object to a primitive variable, Java calls .intValue(), .doubleValue(), or .booleanValue() automatically.

The Autoboxing and Unboxing Cycle

Primitive int, double, boolean Wrapper Object Integer, Double, Boolean Autoboxing .valueOf() Unboxing .intValue() / .doubleValue()

Primitives vs Wrappers: Key Differences

Primitive Wrapper
Type int, double, boolean Integer, Double, Boolean
Default value 0, 0.0, false null
Memory Stack (fast) Heap (object overhead)
Can be null? No Yes
Use in ArrayList No Yes
Equality check == compares values == compares references

How to Use Wrappers in Practice

1
Declare with the wrapper type when you need an object: Integer count instead of int count.
2
Assign primitives directly. Autoboxing handles the conversion: Integer n = 5;
3
Use in collections freely. Write ArrayList<Integer> and add plain ints: the list autoboxes them.
4
Compare values with .equals(), not ==. Two Integer objects with the same value can be different references.
5
Watch for null. Unboxing a null wrapper throws a NullPointerException.

Example 1: ArrayList with Autoboxing

ArrayList<Integer> scores = new ArrayList<>(); scores.add(95); // autoboxing: int → Integer scores.add(87); // autoboxing: int → Integer scores.add(92); // autoboxing: int → Integer int first = scores.get(0); // unboxing: Integer → int // first is now 95

Example 2: Boolean Autoboxing

ArrayList<Boolean> flags = new ArrayList<>(); flags.add(true); // autoboxing: boolean → Boolean flags.add(false); boolean check = flags.get(0); // unboxing if (flags.get(0)) { // unboxing in condition System.out.println("First flag is true"); }

Example 3: The == Trap with Integers

Integer a = 127; Integer b = 127; System.out.println(a == b); // true (cached) Integer c = 200; Integer d = 200; System.out.println(c == d); // false (!) System.out.println(c.equals(d)); // true (correct way)

The Integer Cache Explained

Cached Range: -128 to 127 a = 127 b = 127 Integer 127 Same object: == is true Outside Cache: 200 c = 200 d = 200 Int 200 Int 200 Different objects: == is false

Common Mistakes to Avoid

✕ Using == for Wrappers
Comparing Integer objects with == checks references, not values. Use .equals() or unbox first.
✕ Unboxing null
If a wrapper variable is null, unboxing it throws a NullPointerException. Always check for null before unboxing.
✕ ArrayList<int>
Generics require objects, not primitives. Write ArrayList<Integer>, not ArrayList<int>. This is a compile error.

Summary: Wrapper Classes at a Glance

Concept What It Does Example
Wrapper Class Wraps primitive in an object Integer, Double, Boolean
Autoboxing Primitive → Object (auto) Integer n = 5;
Unboxing Object → Primitive (auto) int x = obj;
.equals() Compares wrapper values a.equals(b)
Integer Cache Reuses objects: -128 to 127 == works only in this range

Question 1 of 5

Which of the following correctly declares an ArrayList that can store integer values?

Question 2 of 5

What is the output of the following code?

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

Question 3 of 5

What happens when the following code runs?

Integer num = null; int val = num;

Question 4 of 5

Which line demonstrates autoboxing?

// Line 1: int a = 10; // Line 2: Integer b = a; // Line 3: int c = b; // Line 4: Integer d = Integer.valueOf(10);

Question 5 of 5

What is the correct way to compare two Double wrapper objects for value equality?