Wrapper Classes & Autoboxing | AP CSA Topic Guide
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.
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.
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
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.
Which statement best describes what occurs at the expression q.get(0) + q.get(1)?
q.get(0) returns and what the + operator does to that type.- (A) A compile-time error occurs because
+is not defined forIntegerobjects. - (B) Each
Integerreturned bygetis automatically unboxed toint, then+performs integer addition. - (C) The two
Integerreferences are concatenated usingtoStringand the resultingStringis parsed back to anint. - (D) A
NullPointerExceptionis thrown becausegetreturns a reference type.
Question 2. Consider the three declarations below.
Which of the declarations will cause a compile-time error?
- (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?
Integer cache range and what each operator (== vs .equals) actually compares.- (A)
truetrue - (B)
falsefalse - (C)
falsetrue - (D)
truefalse
Question 4. A method receives an ArrayList parameter named r and must return the smallest value. Consider the three implementations below.
Assume r contains at least one element and no null entries. Which implementation(s) always return the correct minimum?
- (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.
Which statement best describes the behavior of total when called with an ArrayList that contains a null element?
null.- (A) The method returns 0 because
nullis treated as zero during addition. - (B) The method returns the sum of the non-
nullelements, skipping thenull. - (C) A
NullPointerExceptionis thrown when thenullelement is unboxed during the addition. - (D) A compile-time error occurs because
Integercannot be added to anint.
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?
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
intin anArrayListgeneric: Must beInteger, notint. -
Using
==to compareIntegerobjects: Always use.equals(). -
Unboxing
null: If a list element isnull, assigning it to anintthrowsNullPointerException. -
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.parseIntrejects 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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]