Stop 06 · Core Java APIsTopics · 2/8

Wrapper Classes & Autoboxing

Part of the Java Roadmap

In short

Collections like ArrayList and HashMap can only hold objects, not raw int or double values. Wrapper classes bridge that gap, and autoboxing does it silently, which is handy until a null sneaks in.

Each primitive has a wrapper class (Integer, Double, Boolean and so on) so it can be used in collections. Autoboxing converts between them automatically, but a null wrapper causes a NullPointerException when unboxed.

The jaadu packs loose ladoos into small gift boxes and stacks them on a sweet shop shelf, while one empty box sits open at the end.

Think of it like this

Ladoos in gift boxes

A loose ladoo (int) is quick to eat but can't go on the shop's display shelf. Put it in a small gift box (Integer) and it fits the shelf (a collection). The shopkeeper boxes and unboxes for you automatically. But an empty box (null) has no ladoo inside, and trying to eat from it ends badly.

Watch it happen

Which Integer values are cached?

-128
0
...
1
0
2
...
3
127
4
128
5
1000
6

1/4Integer.valueOf() (used by autoboxing) keeps ready-made objects for -128 to 127.

The big ideas

What you need to know

One wrapper per primitive

int has Integer, double has Double, char has Character, boolean has Boolean, and so on. They are immutable objects.

Autoboxing and unboxing

list.add(87) boxes an int into an Integer. int x = list.get(0) unboxes it back. The compiler inserts the calls for you.

null can't unbox

A wrapper can be null; a primitive can't. Unboxing null throws NullPointerException, often from map.get() on a missing key.

Useful helpers

Integer.parseInt("250"), Double.parseDouble, Integer.MAX_VALUE and Integer.compare live on the wrapper classes.

See it in code

Walk through the code

java
1import java.util.ArrayList;2import java.util.HashMap;3import java.util.List;4import java.util.Map;5 6public class WrapperDemo {7    public static void main(String[] args) {8        List<Integer> marks = new ArrayList<>();9        marks.add(87);             // autoboxing: int -> Integer10        int first = marks.get(0);  // unboxing: Integer -> int11 12        Integer a = 127, b = 127;13        Integer c = 1000, d = 1000;14        System.out.println(a == b);       // true (cached object)15        System.out.println(c == d);       // false (two separate objects)16        System.out.println(c.equals(d));  // true17 18        Map<String, Integer> stock = new HashMap<>();19        Integer qty = stock.get("Parle-G");   // null, key missing20        System.out.println(qty);              // null21        int safe = stock.getOrDefault("Parle-G", 0);22        // int boom = stock.get("Parle-G");   // NullPointerException!23        int total = Integer.parseInt("250") + safe + first;24        System.out.println(total);            // 33725    }26}

1/4A List<Integer> holds objects. Adding 87 boxes it; reading it back into an int unboxes it.

Boxing, the Integer cache trap and the null unboxing trap, in one small kirana-stock example.

Save yourself the pain

Common mistakes

Oops

Comparing two Integer objects with ==.

Do this instead

Use a.equals(b) or compare after unboxing. == only works by accident for values in the -128 to 127 cache.

Oops

int count = map.get(key); when the key might be missing.

Do this instead

Use map.getOrDefault(key, 0) or check for null first, so unboxing never hits a null.

Oops

Using Integer and Double for counters inside hot loops.

Do this instead

Prefer primitives for arithmetic. Each boxing can create an object, which slows down tight loops.

Your mission

Box, unbox, crash, fix

~15 min

Quick check

What happens when you run: Integer x = null; int y = x;

Go deeper

  1. 1Store ints in an ArrayList<Integer> and see autoboxing at work.
  2. 2Compare two Integer objects with == and with equals() for values above 127.
  3. 3Unbox a null Integer to see the NullPointerException.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge