Wrapper Classes & Autoboxing | AP CSA Topic Guide

Unit 1 — Using Objects & Methods

Wrapper Classes & Autoboxing New in 2026

Wrapper classes bridge the gap between Java's primitive types and its object-oriented type system. Understanding them is essential for using ArrayList correctly and is explicitly tested in the 2025-2026 AP CSA curriculum.

The Three Exam-Relevant Wrapper Classes

Primitive Wrapper Class Key Static Methods / Constants
int Integer Integer.parseInt(String), Integer.MIN_VALUE, Integer.MAX_VALUE
double Double Double.parseDouble(String)
boolean Boolean Boolean.parseBoolean(String)

Why Wrapper Classes Exist

Java generics only work with reference types. Primitives (int, double, boolean) are NOT reference types — they live on the stack, not the heap. Wrapper classes are the reference-type version of each primitive.

ArrayList<int> list = new ArrayList<>(); // COMPILE ERROR ArrayList<Integer> list = new ArrayList<>(); // correct

Autoboxing: Primitive to Wrapper

Java automatically wraps a primitive in its wrapper class when a reference type is expected. You don't have to write new Integer(5) — Java does it for you.

ArrayList<Integer> list = new ArrayList<>(); list.add(42); // autoboxing: 42 (int) -> Integer(42) list.add(7); int first = list.get(0); // unboxing: Integer(42) -> 42 (int)

Unboxing: Wrapper to Primitive

Unboxing is the reverse: Java automatically extracts the primitive value from a wrapper object.

Integer x = 100;   // autoboxing
int y = x;         // unboxing: y = 100
int z = x + 5;     // unboxing happens automatically in arithmetic

NullPointerException trap: Unboxing a null wrapper throws NullPointerException. If an Integer variable is null and you try to unbox it (e.g., assign to int or use in arithmetic), the program crashes at runtime.

Integer.parseInt and Double.parseDouble

These static methods convert a String to a primitive. They are commonly used when reading numeric data from files with Scanner.

String s = "123";
int n = Integer.parseInt(s);            // n = 123
double d = Double.parseDouble("3.14");  // d = 3.14

// Reading a number from a line of file input
String line = sc.nextLine();
int value = Integer.parseInt(line.trim());

Integer.MIN_VALUE and Integer.MAX_VALUE

These constants represent the extreme values of an int. They are useful as starting values in max/min accumulator patterns.

int largest = Integer.MIN_VALUE;  // start below any real value
for (int v : arr)
{
    if (v > largest)
        largest = v;
}
Constant Value Use
Integer.MIN_VALUE -2,147,483,648 Initialize a max-finder so the first element wins
Integer.MAX_VALUE 2,147,483,647 Initialize a min-finder so the first element wins

== vs .equals() with Wrapper Objects

This is the most common exam trap for wrapper classes. == compares references; .equals() compares values. Java caches Integer objects from -128 to 127, so == may return true for small values but false for larger ones — an inconsistent behavior you must never rely on.

Integer a = 127;
Integer b = 127;
System.out.println(a == b);       // true  (cached)
System.out.println(a.equals(b));  // true

Integer c = 200;
Integer d = 200;
System.out.println(c == d);       // false (not cached - new objects)
System.out.println(c.equals(d));  // true

Always use .equals() to compare Integer, Double, or any wrapper object values. Never rely on == for wrapper comparison — behavior depends on caching rules and produces unpredictable results on the exam.

Wrapper Classes in ArrayList Operations

ArrayList<Integer> list = new ArrayList<>(); list.add(85); // autoboxing list.add(92); list.add(78); int total = 0; for (int v : list) // unboxing each iteration { total += v; } double avg = (double) total / list.size();

Practice MCQs

These questions follow AP exam difficulty: predict the answer before reading the choices, watch for spot-the-error and multi-correct (I/II/III) formats, and notice that variable names give away nothing about what the code does.

Question 1. Consider the following code segment.

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

Which statement best describes what occurs at the expression q.get(0) + q.get(1)?

Predict first: Before reading the choices, decide what type q.get(0) returns and what the + operator does to that type.
  • (A) A compile-time error occurs because + is not defined for Integer objects.
  • (B) Each Integer returned by get is automatically unboxed to int, then + performs integer addition.
  • (C) The two Integer references are concatenated using toString and the resulting String is parsed back to an int.
  • (D) A NullPointerException is thrown because get returns a reference type.

Question 2. Consider the three declarations below.

I. ArrayList<int> r = new ArrayList<>(); II. ArrayList<Integer> r = new ArrayList<>(); III. Integer r = 5;

Which of the declarations will cause a compile-time error?

Predict first: Recall that generic type parameters must be reference types, and that primitive literals can autobox into wrapper variables.
  • (A) I only
  • (B) II only
  • (C) I and III
  • (D) II and III

Question 3. Consider the following code segment.

Integer p = 200;
Integer q = 200;
System.out.println(p == q);
System.out.println(p.equals(q));

What is printed?

Predict first: Recall the Integer cache range and what each operator (== vs .equals) actually compares.
  • (A) true
    true
  • (B) false
    false
  • (C) false
    true
  • (D) true
    false

Question 4. A method receives an ArrayList parameter named r and must return the smallest value. Consider the three implementations below.

I. int m = 0; for (Integer v : r) if (v < m) m = v; return m; II. int m = Integer.MAX_VALUE; for (int v : r) if (v < m) m = v; return m; III. int m = Integer.MIN_VALUE; for (int v : r) if (v < m) m = v; return m;

Assume r contains at least one element and no null entries. Which implementation(s) always return the correct minimum?

Predict first: Test each starting value against an all-positive list and against a list whose minimum is negative.
  • (A) I, II, and III
  • (B) I and II only
  • (C) II and III only
  • (D) II only

Question 5. Consider the method below, which is intended to return the sum of all values in an ArrayList.

public static int total(ArrayList<Integer> r) { Integer t = 0; for (int i = 0; i < r.size(); i++) { t = t + r.get(i); } return t; }

Which statement best describes the behavior of total when called with an ArrayList that contains a null element?

Predict first: Identify exactly which line forces unboxing and what happens if the value being unboxed is null.
  • (A) The method returns 0 because null is treated as zero during addition.
  • (B) The method returns the sum of the non-null elements, skipping the null.
  • (C) A NullPointerException is thrown when the null element is unboxed during the addition.
  • (D) A compile-time error occurs because Integer cannot be added to an int.

Question 6. Consider the following three statements.

I.   double d = Double.parseDouble("3.14");
II.  int n = Integer.parseInt("twelve");
III. int k = Integer.parseInt("42 ");

Which of the statements will execute without throwing an exception at runtime?

Predict first: Recall which characters parseInt accepts. Whitespace? Letters? A decimal point?
  • (A) I, II, and III
  • (B) I only
  • (C) I and III only
  • (D) II and III only

Common Mistakes

  • Using int in an ArrayList generic: Must be Integer, not int.
  • Using == to compare Integer objects: Always use .equals().
  • Unboxing null: If a list element is null, assigning it to an int throws NullPointerException.
  • Initializing min with 0 instead of Integer.MAX_VALUE: If all data values are positive, a min accumulator starting at 0 never updates.
  • Forgetting that Integer.parseInt rejects whitespace: Trim the string first if it may have leading or trailing spaces.

Related Topics

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]