AP Computer Science A · Study guide

Wrapper Classes

Unit 4 · Data Collections · Topic 4.7

Wrapper Classes

Why an Integer can never be edited, the exact four moments the compiler converts for you, and why parsing is a different thing entirely.

Exam-day recap
  • int is a primitive value; Integer is an object reached through a reference, so it can be null and go in an ArrayList.
  • Integer and Double are part of java.lang, which is available by default, so no import is needed.
  • An Integer object is immutable: once created, its attributes cannot be changed.
  • count = count + 1 reads the value, adds 1, and puts the result in a different object; the first object is unchanged.
  • Autoboxing converts a primitive to its wrapper when passed as a parameter expecting the wrapper, or assigned to a wrapper variable.
  • Unboxing converts a wrapper to its primitive when passed as a parameter expecting the primitive, or assigned to a primitive variable.
  • In Integer sum = a + b; with Integer operands, both unbox for + and the int result autoboxes on assignment.
  • Integer.parseInt(String s) returns the String argument as an int; Double.parseDouble returns it as a double.
  • Both parse methods are class methods, called on the class rather than on an object.
  • Boxing converts a number's type; parsing reads text — if a value is in quotation marks it needs parseInt or parseDouble.

1Two Types for the Same Number

Java has int and it has Integer. They hold the same value and they are not the same thing.

int a = 42;         // a primitive — a value, and nothing else
Integer b = 42;     // an object — a reference to something on the heap
intInteger
What it isA primitive valueAn object, reached through a reference
Can be nullNoYes — it is a reference type
Goes in an ArrayListNoYes
Has methodsNoYes — including parseInt
The learning objective

4.7.A — develop code to use Integer and Double objects from their primitive counterparts, and determine the result of using these objects. Five essential knowledge statements: what the classes are and that they are immutable, exactly when autoboxing happens, exactly when unboxing happens, and the two parse methods.

1.1 No Import Needed

The Integer and Double classes are part of the java.lang package, and classes in java.lang are available by default. Nothing has to be imported — unlike Scanner or File, which do.

2Both Wrapper Classes Are Immutable

Essential knowledge 4.7.A.1

The Integer class and Double class are part of the java.lang package. An Integer object is immutable, meaning once an Integer object is created, its attributes cannot be changed. A Double object is immutable, meaning once a Double object is created, its attributes cannot be changed.

This is the statement that gets skipped, and it is the one that explains what actually happens when a wrapper appears to change.

Integer count = 5;
count = count + 1;      // count now reports 6

Nothing about the original object changed. The value inside it was read, 1 was added to that value, and the result was put into a different object. The variable count was then pointed at the new one. The first object is exactly as it was — it is simply no longer being looked at.

2.1 The Same Rule You Already Met on String

A String object is immutable for exactly the same reason (1.15.A.3), and the consequence is identical: a method or an operator that seems to modify one is producing a new object and re-pointing the variable. If you can already explain why s.toUpperCase() does not change s, you can explain this.

3Autoboxing — Exactly When

Essential knowledge 4.7.A.2

Autoboxing is the automatic conversion that the Java compiler makes between primitive types and their corresponding object wrapper classes. This includes converting an int to an Integer and a double to a Double. The Java compiler applies autoboxing when a primitive value is:

  • passed as a parameter to a method that expects an object of the corresponding wrapper class
  • assigned to a variable of the corresponding wrapper class

Two triggers, and they are worth reading as a closed list rather than as “the compiler sorts it out”. Both are visible in ordinary list code:

ArrayList<Integer> scores = new ArrayList<Integer>();

scores.add(95);     // trigger 1: an int passed where an Integer is expected

Integer best = 95;  // trigger 2: an int assigned to an Integer variable

4Unboxing — Exactly When

Essential knowledge 4.7.A.3

Unboxing is the automatic conversion that the Java compiler makes from the wrapper class to the primitive type. This includes converting an Integer to an int and a Double to a double. The Java compiler applies unboxing when a wrapper class object is:

  • passed as a parameter to a method that expects a value of the corresponding primitive type
  • assigned to a variable of the corresponding primitive type

The same two shapes, read in the other direction:

int first = scores.get(0);   // trigger 2: an Integer assigned to an int variable

printScore(scores.get(1));   // trigger 1: an Integer passed where an int is expected
Why the arithmetic works

Integer sum = a + b; uses both directions in one line: a and b unbox to int because + operates on primitives, the addition happens, and the result autoboxes back because it is being assigned to an Integer variable.

4.1 A Wrapper Variable Can Hold null

Because a wrapper is a reference type, an Integer variable can refer to no object at all. A primitive int never can. That difference is a direct consequence of the first table in this guide, and it is worth noticing when a wrapper arrives from somewhere you did not write.

5Turning Text Into Numbers

Essential knowledge 4.7.A.4 and 4.7.A.5

static int parseInt(String s) returns the String argument as an int.

static double parseDouble(String s) returns the String argument as a double.

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

Both are class methods — called on the class, not on an object, which is why they read Integer.parseInt rather than through a variable.

5.1 Parsing Is Not Boxing

These get conflated, and they take different inputs and produce different outputs:

Starts withEnds withWho does it
Autoboxingint 42Integer objectThe compiler, automatically
parseIntString "42"int 42You, by calling it
The tell

If the starting value is in quotation marks, it is text, and no amount of boxing will turn it into a number — that needs parseInt or parseDouble. This is exactly the situation after .split() on a line read from a file, where every piece arrives as a String.

6What Belongs to This Topic, and What Does Not

Two things commonly taught alongside wrappers are real course content that lives under other objectives. Knowing where they come from keeps an answer precise:

IdeaWhere it actually lives
Integer.MAX_VALUE / MIN_VALUE1.5.B — the range of int, and when an expression goes out of it
NullPointerException1.14.A.2 — a method call on a null reference
Why an ArrayList needs a wrapper4.8.A and this topic together — a list holds objects, so a primitive cannot go in one directly

7Check Yourself

After Integer n = 7; n = n + 1; — was the original object changed?

Response

No. An Integer is immutable, so its attributes cannot change once it is created. The value was read out, 1 was added, and the result went into a new object that n now refers to.

Name the two situations in which the compiler autoboxes.

Response

When a primitive is passed as a parameter to a method expecting the corresponding wrapper class, and when a primitive is assigned to a variable of the corresponding wrapper class.

Does Integer need an import?

Response

No. It is part of java.lang, and classes in that package are available by default — unlike Scanner and File.

You have the String "88" and you need it as a number. Does autoboxing help?

Response

No. Autoboxing converts between a primitive and its wrapper; it does not read text. Turning "88" into a number is Integer.parseInt("88").

8Summary

IdeaWhat to remember
The pairint/Integer and double/Double
Packagejava.lang — available by default, no import
ImmutableOnce created, a wrapper object's attributes cannot be changed
AutoboxingPrimitive → wrapper, on a parameter or an assignment
UnboxingWrapper → primitive, on a parameter or an assignment
parseIntString → int, called on the class
parseDoubleString → double, called on the class
Not the sameBoxing converts a number's type; parsing converts text into a number
The one sentence

A wrapper is the object form of a number, it can never be edited once it exists, and the compiler moves values between the two forms automatically in exactly four situations — two in each direction, both of them a parameter or an assignment.