BackendBench
Coffee

Unravel › Java concepts › Numbers and math

Java language

Autoboxing and wrapper types

Each primitive has an object twin (int and Integer).

Learn these first

At a glance

Mental model

A coin and a coin in a presentation box.

What happens, in order
  1. 1Collections and generics hold objects only: List<Integer>, Map<String, Long>list.add(5) boxes 5 into an Integer; int x = list.get(0) unboxes it.
  2. 2Unboxing a null throws NullPointerException: Integer count = map.get(key); int n = count; fails when the key is missing
  3. 3Integer a = 127, b = 127; a == b is true but Integer a = 1000, b = 1000; a == b is false: small values come from a cache and larger ones are separate objectsAlways use equals (or unbox) for wrapper comparison.
  4. 4Integer.parseInt("42") returns a primitive; Integer.valueOf("42") returns the boxed objectBoth throw NumberFormatException on bad text.
Remember
Watch out
Comparing Integer objects with ==Unboxing a value that can be nullConfusing remove(int index) with remove(Object) on a List<Integer>
Example (highlighted lines explain it)
import java.util.*;public class Counts {  static int total(Map<String, Integer> counts, String key) {    Integer n = counts.get(key);    return n == null ? 0 : n.intValue();  }  static boolean same(Integer a, Integer b) { return a.equals(b); }  static int parse(String s) { return Integer.parseInt(s.trim()); }}
  1. line 3 uses Integer, the core of Autoboxing and wrapper types
  2. line 4 uses Integer, the core of Autoboxing and wrapper types
  3. line 5 uses intValue, the core of Autoboxing and wrapper types
  4. line 7 uses Integer, the core of Autoboxing and wrapper types
  5. line 8 uses Integer, the core of Autoboxing and wrapper types
Read the full explanation (about 1 min)

Each primitive has an object twin (int and Integer). Java converts automatically (autoboxing), which is why you can put int values into a List<Integer>, but the objects can be null and are slower.

A mental model

A coin and a coin in a presentation box. The box lets the coin go into places that only accept boxes (collections, generics), but it costs space and the box can be empty (null).

How it works

Collections and generics hold objects only: List<Integer>, Map<String, Long>. list.add(5) boxes 5 into an Integer; int x = list.get(0) unboxes it.

Unboxing a null throws NullPointerException: Integer count = map.get(key); int n = count; fails when the key is missing.

Integer a = 127, b = 127; a == b is true but Integer a = 1000, b = 1000; a == b is false: small values come from a cache and larger ones are separate objects. Always use equals (or unbox) for wrapper comparison.

Integer.parseInt("42") returns a primitive; Integer.valueOf("42") returns the boxed object. Both throw NumberFormatException on bad text.

Going deeper

Boxing inside a hot loop creates garbage: Long sum = 0L; for (...) sum += x; allocates a new Long each iteration. Use the primitive or the specialised streams (IntStream).

Overloads can surprise: list.remove(1) removes by index while list.remove(Integer.valueOf(1)) removes the value 1.

A ternary mixing int and Integer unboxes: flag ? 1 : nullInteger throws a NullPointerException.

Under the hood

Boxing compiles to Integer.valueOf(int), which returns cached instances for -128 to 127 (configurable with -XX:AutoBoxCacheMax). Unboxing compiles to intValue(). An Integer object costs about 16 bytes versus 4 for an int.

Common mistakes

  • Comparing Integer objects with ==.
  • Unboxing a value that can be null.
  • Confusing remove(int index) with remove(Object) on a List<Integer>.

How Unravel recognises it

When you paste code, Unravel looks for these signs of autoboxing and wrapper types. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Autoboxing and wrapper types?

Each primitive has an object twin (int and Integer). Java converts automatically (autoboxing), which is why you can put int values into a List<Integer>, but the objects can be null and are slower.

What are the most common mistakes with Autoboxing and wrapper types?

Comparing Integer objects with ==. Unboxing a value that can be null. Confusing remove(int index) with remove(Object) on a List<Integer>.

How can I see Autoboxing and wrapper types in real code?

Paste your own Java into Unravel and it will point out where autoboxing and wrapper types appears, explain how it works at the detail level you choose, and draw the flow of the code around it.