Part 04 / Topic 6 of 10
Autoboxing: how a List<Integer> can hold int values
Collections hold objects. int is a primitive, not an object — so how does quantities.add(5) work on an ArrayList<Integer>? Java quietly wraps it for you.
- Autoboxing: Java automatically wraps a primitive (
5) into its object form (Integer.valueOf(5)) when an object is expected — for example, passing it toadd(...). - Unboxing: Java automatically unwraps an
Integerback into anintwhen a primitive is expected — for example, a for-each loop variable declared asint.
Every primitive has a matching wrapper class: int ↔ Integer, double ↔ Double, boolean ↔ Boolean, char ↔ Character. You rarely write the conversion yourself; the compiler inserts it.
import java.util.ArrayList;
public class Autoboxing {
public static void main(String[] args) {
ArrayList<Integer> quantities = new ArrayList<>();
quantities.add(5); // int 5 autoboxed to an Integer object
quantities.add(2);
quantities.add(10);
int total = 0;
for (int qty : quantities) { // each Integer auto-unboxed back to int
total += qty;
}
System.out.println("Total quantity: " + total);
// Java caches small Integer objects (-128 to 127), so two boxed values
// in that range can be the SAME object.
Integer a = 100;
Integer b = 100;
System.out.println(a == b); // true: same cached object
Integer c = 200;
Integer d = 200;
System.out.println(c == d); // false: outside the cache, two different objects
System.out.println(c.equals(d)); // true: equals() always compares the value
// A missing quantity stored as null compiles fine...
Integer maybeQty = null;
quantities.add(maybeQty);
System.out.println(quantities);
// ...but unboxing it crashes: there is no int value inside null.
System.out.println("Unboxing a null Integer now:");
int boom = quantities.get(3);
System.out.println(boom);
}
}
Output and crash, in order (examples/Autoboxing.java):
Total quantity: 17
true
false
true
[5, 2, 10, null]
Unboxing a null Integer now:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.ArrayList.get(int)" is null
at Autoboxing.main(Autoboxing.java:34)
Two usage-level facts to keep, nothing deeper:
Integer a = 100andInteger b = 100can bea == b(true) only because Java caches smallIntegerobjects from-128to127. The moment the value is outside that range (200), autoboxing makes two separate objects, and==becomesfalseeven though the values are equal. This is exactly why you compare wrapper objects with.equals(...), never==— the cache makes==look safe for small numbers and silently wrong for bigger ones.- An
Integervariable can benull(it's an object); anintvariable never can (it's a primitive). The moment Java tries to unbox anullIntegerinto anint— here, assigningquantities.get(3)to theint boom— it throwsNullPointerException, because there is no primitive value hiding insidenull.
Full program: examples/Autoboxing.java.
Done here? The practice set for Part 4 — including the solutions — is on the part page.