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.
intis a primitive value;Integeris an object reached through a reference, so it can benulland go in anArrayList.- 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 + 1reads 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;withIntegeroperands, both unbox for+and theintresult autoboxes on assignment. Integer.parseInt(String s)returns the String argument as anint;Double.parseDoublereturns it as adouble.- 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
parseIntorparseDouble.
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
int | Integer | |
|---|---|---|
| What it is | A primitive value | An object, reached through a reference |
Can be null | No | Yes — it is a reference type |
Goes in an ArrayList | No | Yes |
| Has methods | No | Yes — including parseInt |
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
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
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
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
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
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 with | Ends with | Who does it | |
|---|---|---|---|
| Autoboxing | int 42 | Integer object | The compiler, automatically |
parseInt | String "42" | int 42 | You, by calling it |
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:
| Idea | Where it actually lives |
|---|---|
Integer.MAX_VALUE / MIN_VALUE | 1.5.B — the range of int, and when an expression goes out of it |
NullPointerException | 1.14.A.2 — a method call on a null reference |
Why an ArrayList needs a wrapper | 4.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?
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.
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?
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?
No. Autoboxing converts between a primitive and its wrapper; it does not read text. Turning
"88" into a number is
Integer.parseInt("88").
8Summary
| Idea | What to remember |
|---|---|
| The pair | int/Integer and double/Double |
| Package | java.lang — available by default, no import |
| Immutable | Once created, a wrapper object's attributes cannot be changed |
| Autoboxing | Primitive → wrapper, on a parameter or an assignment |
| Unboxing | Wrapper → primitive, on a parameter or an assignment |
parseInt | String → int, called on the class |
parseDouble | String → double, called on the class |
| Not the same | Boxing converts a number's type; parsing converts text into a number |
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.