01 / 21
42

Wrapper
Classes

Learn how Java wraps primitives inside objects, enabling them to work with collections, generics, and the powerful features of the object-oriented world.

AP Computer Science A

What Are Wrapper Classes?

Java has eight primitive types (like int, double, boolean) that store raw values directly in memory. But many parts of Java, such as ArrayLists and generics, require objects, not primitives.

Why Do Wrappers Exist?

Collections
ArrayList<Integer> works, but ArrayList<int> does not compile.
Null Values
Primitives cannot be null. Wrapper objects can represent "no value."
Utility Methods
Integer.parseInt() and Double.parseDouble() convert Strings to numbers.

Primitive → Object Mapping

PRIMITIVE WRAPPER CLASS int Integer double Double boolean Boolean char Character AP Exam: Integer & Double

Autoboxing & Unboxing

Java automatically converts between primitives and their wrapper objects. This two-way conversion is called autoboxing and unboxing.

Autoboxing
Primitive → Object
int → Integer, automatically
Unboxing
Object → Primitive
Integer → int, automatically

How Autoboxing Works

PRIMITIVE int x = 5 AUTOBOXING WRAPPER OBJECT Integer obj UNBOXING PRIMITIVE int y = obj WRAPPER OBJECT Integer obj Java compiler inserts Integer.valueOf() and .intValue() automatically

Primitive vs Wrapper Object

Primitive (int) Wrapper (Integer)
Stores Raw value on stack Object reference on heap
Default value 0 null
In ArrayList? ✕ No ✓ Yes
Equality check == compares values == compares references
Can be null? ✕ No ✓ Yes

How to Use Wrapper Classes

1
Declare the ArrayList with the wrapper type: ArrayList<Integer> or ArrayList<Double>.
2
Add primitives directly: list.add(42) autoboxes 42 into an Integer object.
3
Retrieve and use: int val = list.get(0) auto-unboxes the Integer back to int.
4
Compare values with .equals(), not ==, when comparing two wrapper objects directly.

Example: ArrayList with Integers

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

Example: Arithmetic with Wrappers

Integer a = 10; // autoboxing Integer b = 20; // autoboxing int sum = a + b; // unboxes both, adds, result is int System.out.println(sum); // prints 30 Double price = 9.99; // autoboxing double → Double double tax = price * 0.08; // unboxes, multiplies System.out.println(tax); // prints 0.7992

The == Trap with Wrappers

Integer x = 200; Integer y = 200; System.out.println(x == y); // false! Compares references System.out.println(x.equals(y)); // true! Compares values Integer m = 5; Integer n = 5; System.out.println(m == n); // true (cached range -128 to 127)

Key Methods to Know

Integer.parseInt(s)
Converts a String to a primitive int. Throws NumberFormatException if invalid.
Double.parseDouble(s)
Converts a String to a primitive double. Works with decimals.
.intValue()
Returns the int stored in an Integer object. Rarely needed due to unboxing.
.doubleValue()
Returns the double stored in a Double object. Also rarely called explicitly.

Parsing Strings to Numbers

String input = "42"; int num = Integer.parseInt(input); // num = 42 String priceStr = "19.99"; double price = Double.parseDouble(priceStr); // price = 19.99 // This throws NumberFormatException at runtime: int bad = Integer.parseInt("hello"); // ERROR!

NullPointerException Risk

Integer val = null; int x = val; // NullPointerException at runtime! // Java tries to unbox null → crash

This commonly happens when an ArrayList<Integer> contains a null element and you try to assign it to a primitive int variable.

Common Mistakes

✕ Using == for Equality
== compares object references, not values. Use .equals() to compare wrapper objects.
✕ ArrayList<int>
Primitives cannot be type arguments. Write ArrayList<Integer>, not ArrayList<int>.
✕ Unboxing null
Assigning a null Integer to an int causes NullPointerException at runtime.

Summary Reference

Concept Details
AP Wrapper Types Integer, Double
Autoboxing int → Integer (automatic)
Unboxing Integer → int (automatic)
Parse Methods Integer.parseInt(), Double.parseDouble()
Equality Use .equals(), not ==
Null Danger Unboxing null → NullPointerException
Cache Range Integer caches -128 to 127

Question 1 of 5

Which of the following is a valid ArrayList declaration in Java?

Question 2 of 5

What is the output of the following code?

Integer a = 200;
Integer b = 200;
System.out.println(a == b);

Question 3 of 5

What does Integer.parseInt("37") return?

Question 4 of 5

What happens when this code runs?

Integer val = null;
int x = val;

Question 5 of 5

Consider this code:

ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(10);
nums.add(20);
int sum = nums.get(0) + nums.get(1);


Which of the following best describes what happens?