5m left·0%
Reading Time: 5 min
Last Updated: March 17, 2026
Main Ideas: 5
Reading Time: 5 min
Last Updated: March 17, 2026
Main Ideas: 5

Topic 4.7 Notes – Wrapper Classes

Verified for 2027 AP® Computer Science A Exam
Read aloud
Wrapper classes let you treat primitive values like objects. In AP CSA, the two that matter are Integer (for int) and Double (for double). Most questions focus on how Java automatically converts between primitives and wrappers and what happens when that conversion goes wrong.

1. What Wrapper Classes Are

A wrapper class is the object version of a primitive type.

  • int → Integer
  • double → Double

They’re in the java.lang package, which means you don’t import them.

Primitive vs Wrapper

  • A primitive stores a raw value directly.
  • A wrapper stores a value inside an object, and the variable holds a reference to that object.

That difference explains almost every wrapper bug you’ll see.

Immutability

Both Integer and Double are immutable.

Integer x = 10;
x = x + 5;

You didn’t change the old object. You created a new Integer and reassigned x.

2. Autoboxing and Unboxing

Java quietly converts between primitives and wrappers for you. This is where most test questions live.

a. Autoboxing (primitive → wrapper)

Autoboxing happens when Java turns a primitive into its wrapper automatically.

Integer a = 5;      // int → Integer
Double d = 3.2;     // double → Double

This also happens when:

  • Passing a primitive to a method that expects a wrapper
  • Adding to an ArrayList<Integer>
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(8);   // int autoboxed to Integer

Behind the scenes, Java is doing something like:

Integer.valueOf(5);

You don’t write it. The compiler inserts it.

b. Unboxing (wrapper → primitive)

Unboxing is the reverse.

Integer x = 12;
int y = x;   // Integer → int

Also happens in arithmetic:

Integer num = 4;
int total = num + 6;  // num unboxed first

Java automatically extracts the primitive value.

The Null Danger

This is the most common runtime crash with wrappers.

Integer value = null;
int n = value;   // NullPointerException

Java tries to unbox null. There is no value inside. Program crashes.

If you ever see arithmetic with Integer variables, mentally ask yourself, “Could this be null?”

This shows up a lot with ArrayList<Integer> because lists can store null.

3. Integer and Double Methods You Must Know

These are on the Java Quick Reference.

a. Integer.parseInt(String s)

int num = Integer.parseInt("42");
  • Takes a String
  • Returns a primitive int
  • Used when converting text to numbers

Common situation: processing user input or splitting a string.

Important detail: it returns int, not Integer.

b. Double.parseDouble(String s)

double val = Double.parseDouble("3.14");
  • Takes a String
  • Returns a primitive double

Same idea as parseInt, just for decimals.

Wrapper vs Primitive Return

Keep this straight:

  • parseInt → int
  • parseDouble → double

They do not return wrapper objects.

4. Comparing Integer Objects Correctly

This is a classic AP multiple-choice trap.

== vs equals()

ComparisonWhat it checksCorrect for Integer?
==Same object (same reference)No
equals()Same stored valueYes

Example:

Integer a = 150;
Integer b = 150;

System.out.println(a == b);        // false
System.out.println(a.equals(b));   // true

Why?

  • == checks if both variables point to the same object.
  • equals() checks if the values inside are equal.

Rule you should hear in your head during tests:

  • Use == for primitives.
  • Use equals() for wrapper objects.

If you mix this up on an MCQ, the wrong answer will look very tempting.

5. How Wrapper Classes Show Up on Tests

ArrayList<Integer> Patterns

ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(3);           // autoboxing
int x = nums.get(0);   // unboxing

Watch for:

  • null values in the list
  • Arithmetic with Integer
  • Comparisons using == instead of equals()

Code Tracing Strategy

When tracing:

  1. Identify every autoboxing.
  2. Identify every unboxing.
  3. Check for possible null.
  4. Check whether comparisons use == or equals().

A lot of wrapper questions aren’t hard. They’re subtle.

Key Takeaways

Integer and Double are immutable object versions of int and double.
Autoboxing converts primitive → wrapper automatically.
Unboxing converts wrapper → primitive automatically.
Unboxing null causes a NullPointerException at runtime.
parseInt and parseDouble return primitives, not wrapper objects.
Use equals() to compare wrapper objects and == for primitives.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining