AP® Computer Science A · Unit 4 · Topic 4.7

Integer, Double, Autoboxing and Unboxing

Autoboxing is Java automatically wrapping a primitive like int inside its object version, Integer, and unboxing is the reverse; collections like ArrayList need this because they can only store objects, never primitives.

An ArrayList stores objects, full stop. A plain int is a primitive, not an object, so ArrayList<Integer> cannot hold raw ints directly. Java bridges this gap automatically: when you write list.add(5), the compiler quietly converts that 5 into an Integer object before storing it. That conversion is autoboxing. Unboxing runs the other way: when you pull an Integer out of a list and use it in arithmetic, Java automatically extracts the plain int value so the math works. You never write the conversion code yourself; the compiler inserts it.

This convenience hides a real object underneath. Integer objects behave like any other object when compared with ==, which checks whether two variable names point to the same memory location, not whether they hold equal values. Java caches small boxed values from -128 to 127 for efficiency, so two Integers in that range often do point to the same cached object and == can accidentally return true. Outside that range, each boxing creates a fresh object, and == correctly returns false even when the values match, since .equals() is the method meant for comparing value equality.

Unboxing also fails loudly when there is nothing to unbox. A null Integer has no int value hiding inside it, so any arithmetic or comparison that forces unboxing throws a NullPointerException. This matters constantly in Unit 4, where ArrayLists of Integer get searched, sorted, and summed, autoboxing and unboxing on nearly every line without announcing itself.

Key terms

Autoboxing
Java's automatic conversion of a primitive value, like int, into its matching object type, like Integer.
Unboxing
Java's automatic conversion of a wrapper object, like Integer, back into its primitive value for use in arithmetic.
Wrapper class
An object type such as Integer or Double that holds a primitive value inside an object so it can be used where objects are required.
Integer cache
Java's reuse of the same Integer object for boxed values from -128 to 127, which can make == misleadingly return true.
NullPointerException
The error thrown when code tries to unbox a null reference, since there is no primitive value to extract.

Watch it explained

  1. How it worksHow does autoboxing wrap an int inside an Integer object without you writing a line?
  2. Worked exampleTrace list.add(5) on an ArrayList of Integer and find what actually lands in the list.
  3. Worked exampleFind what happens when an Integer that is null gets unboxed into an int.
  4. Common mistakeWhy isn't comparing two Integer objects with == the same as comparing two ints?

Check yourself

Your turn1 of 2
Integer a = 200; Integer b = 200; if (a == b) ... Why might this print false, unlike small values like 100?

End of topic.