Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Why wrapper classes exist
  3. Autoboxing and unboxing
  4. Constants and parsing methods
  5. Comparing wrappers
  6. Try this

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_VALUE and Integer.MAX_VALUE are the smallest and largest values an int can hold (-2147483648 and 2147483647). They are how you refer to the int range in code.
  • Integer.parseInt(str) converts a String of digits to an int. Integer.parseInt("42") is 42.
  • Double.parseDouble(str) converts a numeric String to a double. Double.parseDouble("3.14") is 3.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 int 4 into an Integer object, so the object reference count can 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

Sources & how we know this