Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

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 to add(...).
  • Unboxing: Java automatically unwraps an Integer back into an int when a primitive is expected — for example, a for-each loop variable declared as int.

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:

  1. Integer a = 100 and Integer b = 100 can be a == b (true) only because Java caches small Integer objects from -128 to 127. The moment the value is outside that range (200), autoboxing makes two separate objects, and == becomes false even 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.
  2. An Integer variable can be null (it's an object); an int variable never can (it's a primitive). The moment Java tries to unbox a null Integer into an int — here, assigning quantities.get(3) to the int boom — it throws NullPointerException, because there is no primitive value hiding inside null.

Full program: examples/Autoboxing.java.

Done here? The practice set for Part 4 — including the solutions — is on the part page.

Search the guide