Wrapper Classes
Introduced| English | Meaning |
|---|---|
| wrapper class/ˈræpə klæs/ | In Java, a class whose objects represent values of a corresponding primitive type, such as Integer for int. |
| autoboxing/ˌɔːtəʊˈbɒksɪŋ/ | Java's automatic conversion of a primitive value to a reference to an object of its corresponding wrapper class. |
| unboxing/ʌnˈbɒksɪŋ/ | Java’s automatic conversion from a wrapper reference, such as Integer, to its primitive value; null cannot be unboxed successfully. |
Boxing a primitive
- Some Java structures (like
ArrayList) hold objects, not primitives. - A wrapper class 包装类 wraps a primitive in an object:
Integerwrapsint,Doublewrapsdouble. Integer boxed = Integer.valueOf(5);puts theint5inside an object.- Wrappers let primitives live where only objects are allowed.
Autoboxing and unboxing
- Java converts automatically — autoboxing 自动装箱 and unboxing 自动拆箱.
Integer x = 5;autoboxes5;int y = x;unboxes it back.- So
list.add(5)works even though the list holdsIntegerobjects. - You rarely write
Integer.valueOfby hand — autoboxing does it.

Autoboxing and unboxing between int and Integer
Why ArrayList needs them
ArrayList<Integer>holdsIntegerobjects, because generics require a reference type.- You can't write
ArrayList<int>— primitives aren't objects. - Autoboxing lets you add and read plain
ints conveniently. Double,Boolean, etc. play the same role for their primitives.
Compare wrappers with equals
- Two
Integerobjects should be compared with.equals(), not==. ==compares references;.equals()compares the wrapped values.- (Java caches small Integers, so
==sometimes seems to work — don't rely on it.) - The same "use
.equals()for objects" rule fromStringapplies here.
Wrappers are objects, so compare their values with .equals(), not ==. == tests reference identity; for two Integer objects with the same value it can be false. And you can't parameterize a collection with a primitive — it's ArrayList<Integer>, never ArrayList<int>. Autoboxing bridges the gap so you can still add plain ints.
Wrappers with an ArrayList:
ArrayList<Integer> nums = new ArrayList<Integer>();nums.add(5);— theint5is autoboxed into anInteger.int first = nums.get(0);— theIntegeris unboxed back toint.
A wrapper class (Integer, Double, Boolean) wraps a primitive in an object so it can go where objects are required — like an ArrayList<Integer> (you can't write ArrayList<int>). Autoboxing and unboxing convert automatically. Compare wrapper objects with .equals(), not ==.
Autoboxing and unboxing
int ↔ Integer conversions happen automatically.
Which wrapper class wraps an int?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Integer wraps int; Double wraps double.
Why must an ArrayList of ints be declared ArrayList
Difficulty 1/5 · 100 coins + a small bonus on first completion today
You can't write ArrayList
Two Integer objects should be compared with...
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Wrappers are objects — use .equals() for their values.
In nums.add(5), the int 5 is automatically boxed into an Integer (autoboxing).
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Autoboxing converts the primitive to a wrapper object.
A class that wraps a primitive in an object is a ___ class (one word).
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Integer, Double, Boolean are wrapper classes.