Unravel › Java concepts › Numbers and math
Autoboxing and wrapper types
Each primitive has an object twin (int and Integer).
Learn these first
At a glance
A coin and a coin in a presentation box.
- 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.
- 2Unboxing a null throws NullPointerException: Integer count = map.get(key); int n = count; fails when the key is missing
- 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.
- 4Integer.parseInt("42") returns a primitive; Integer.valueOf("42") returns the boxed objectBoth throw NumberFormatException on bad text.
- Boxing inside a hot loop creates garbage: Long sum = 0L; for (...) sum += x; allocates a new Long each iteration
- Overloads can surprise: list.remove(1) removes by index while list.remove(Integer.valueOf(1)) removes the value 1
- Boxing compiles to Integer.valueOf(int), which returns cached instances for -128 to 127 (configurable with -XX:AutoBoxCacheMax)
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()); }}- line 3 uses Integer, the core of Autoboxing and wrapper types
- line 4 uses Integer, the core of Autoboxing and wrapper types
- line 5 uses intValue, the core of Autoboxing and wrapper types
- line 7 uses Integer, the core of Autoboxing and wrapper types
- 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
Integerobjects with==. - Unboxing a value that can be null.
- Confusing
remove(int index)withremove(Object)on aList<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:
- Types:
IntegerLongDoubleBooleanShort - Method calls:
intValue()longValue()doubleValue()booleanValue()Integer.valueOf()Long.valueOf()Double.valueOf()Integer.compare()Integer.toBinaryString()Integer.toHexString()Integer.parseInt()Long.parseLong()Integer.sum()Integer.max()
Part of a learning path
- Text, numbers and arrays · previous: Integer overflow and exact arithmetic · next: Math and number utilities
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.