What are the Integer and Double wrapper classes, and how do autoboxing, unboxing and their useful constants and methods work?
Topic 2.8 Wrapper Classes: Integer and Double: use the Integer and Double wrapper classes, including autoboxing and unboxing, the MIN_VALUE and MAX_VALUE constants, and parsing methods.
A focused answer to AP CSA Topic 2.8, covering why wrapper classes exist, creating Integer and Double objects, autoboxing and unboxing, Integer.MIN_VALUE and MAX_VALUE, parseInt and parseDouble, and the == versus equals trap for wrappers, with a worked trace.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 2.8) wants you to use the Integer and Double wrapper classes: to know why they exist (to treat a primitive as an object), to use autoboxing and unboxing that convert between primitive and wrapper automatically, to use the constants Integer.MIN_VALUE and Integer.MAX_VALUE, and the parsing methods Integer.parseInt and Double.parseDouble. Wrapper classes received expanded emphasis in the current CED.
Why wrapper classes exist
So you cannot write ArrayList<int>, but you can write ArrayList<Integer>. The wrapper is the object form of the primitive.
Autoboxing and unboxing
For example:
Integer a = 10; // autoboxing: int 10 becomes an Integer
int b = a + 5; // a is unboxed to 10, then 10 + 5 = 15
Double d = 3.5; // autoboxing a double
double e = d * 2; // unboxing, then 3.5 * 2 = 7.0
Constants and parsing methods
The wrapper classes provide useful static members (called on the class name):
Integer.MIN_VALUEandInteger.MAX_VALUEare the smallest and largest values anintcan hold (-2147483648and2147483647). They are how you refer to theintrange in code.Integer.parseInt(str)converts a String of digits to anint.Integer.parseInt("42")is42.Double.parseDouble(str)converts a numeric String to adouble.Double.parseDouble("3.14")is3.14.
Parsing is essential whenever numeric data arrives as text (for example from input) and you need to do arithmetic with it. If the String is not a valid number, parseInt throws a run-time exception.
Comparing wrappers
Because Integer and Double are objects (reference types), the == operator compares references, not values, just like Strings. To compare the values reliably, unbox them or use equals. The exam treats this like the String trap: prefer equals (or compare the unboxed primitives) rather than == for wrapper objects.
Try this
Q1. Write a statement that converts the String "57" to an int and stores it in n. [1 point]
- Cue.
int n = Integer.parseInt("57");
Q2. Explain what autoboxing does in the statement Integer count = 4;. [2 points]
- Cue. It automatically wraps the primitive
int4into anIntegerobject, so the object referencecountcan hold it without an explicit conversion.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. Consider the following statements.
```java
Integer a = 5;
int b = a + 3;
System.out.println(b);
```
What is printed, and what features make this work?
(A) `8`, using autoboxing of `5` into `a` and unboxing of `a` in the arithmetic
(B) `53`, because `a + 3` concatenates
(C) Nothing, because you cannot add an `Integer` to an `int`
(D) `8`, but only because `a` is a primitive
(E) A run-time error, because `Integer` objects cannot be added
Show worked answer →
The answer is (A).
Integer a = 5; uses autoboxing: the primitive 5 is automatically wrapped into an Integer object. In a + 3, the Integer a is automatically unboxed back to the int 5, then 5 + 3 is 8, stored in b. (B) is wrong because + on numeric operands adds, not concatenates. (C) and (E) are wrong because autoboxing and unboxing make mixing Integer and int seamless. (D) is wrong because a is an Integer object, not a primitive.
Markers reward naming autoboxing (primitive to wrapper) and unboxing (wrapper to primitive).
AP 2020 (style)4 marksFree response (code writing). Two Strings `s1` and `s2` are given, each containing only digits (for example `"42"` and `"100"`). Write a code segment that converts each to an `int`, adds them, and prints the sum. Then print the largest value an `int` can hold using the appropriate wrapper-class constant.
Show worked answer →
A 4-point question testing parsing and a wrapper constant.
int n1 = Integer.parseInt(s1);
int n2 = Integer.parseInt(s2);
System.out.println(n1 + n2);
System.out.println(Integer.MAX_VALUE);
Point 1: Integer.parseInt(s1) converts the digit String to an int. Point 2: the same for s2. Point 3: adding the two ints and printing the numeric sum (not a concatenation). Point 4: Integer.MAX_VALUE for the largest int. Forgetting to parse and instead concatenating the Strings would give the wrong result.
Related dot points
- Topic 2.7 String Methods: call the String methods in the AP Java subset (length, substring, indexOf, equals, compareTo), respecting zero-based indexing and the immutability of String objects.
A focused answer to AP CSA Topic 2.7, covering the required String methods length, substring (both forms), indexOf, equals and compareTo, zero-based indexing, the half-open range of substring, why == differs from equals, and String immutability, with a worked trace.
- Topic 1.2 Variables and Data Types: identify the primitive types int, double and boolean, and declare, initialise and use variables of those types.
A focused answer to AP CSA Topic 1.2, covering the difference between primitive and reference types, the three exam primitives int, double and boolean, declaring and initialising variables, valid identifiers, and constants, with a traced worked example.
- Topic 1.5 Casting and Ranges of Variables: use casting to convert between int and double, predict the effect of truncation, and recognize that an int has a finite range that can overflow.
A focused answer to AP CSA Topic 1.5, covering explicit casting between int and double, the truncation that casting to int causes, where the cast applies in an expression, the finite range of an int, and integer overflow, with a traced worked example.
- Topic 2.9 Using the Math Class: call the static Math methods in the AP Java subset (abs, pow, sqrt, random) and generate a random integer or double in a specified range.
A focused answer to AP CSA Topic 2.9, covering the required static Math methods abs, pow, sqrt and random, why Math methods are called on the class, the return types, and the standard formula for generating a random int in a range, with a worked trace.
- Topic 2.2 Creating and Storing Objects (Instantiation): use the new keyword to call a constructor and instantiate an object, choosing the correct constructor by its parameters, and store the result in a reference variable.
A focused answer to AP CSA Topic 2.2, covering the new keyword, constructors and how they initialise an object, choosing a constructor by its parameter list, matching argument types, storing the reference, and the meaning of null, with a worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)