All topics
library
beginner

Primitive Types & Wrapper Classes

Know all 8 primitives, their sizes, default values, and the corresponding wrapper classes.

Java has 8 primitive types: byte (8-bit), short (16-bit), int (32-bit), long (64-bit), float (32-bit), double (64-bit), char (16-bit Unicode), and boolean. They store values directly on the stack (for locals) or inline in objects, not as heap-allocated references.

Primitive = a number written on a sticky note (lightweight, no overhead). Wrapper = the same number in a sealed envelope (heavier, can be null, has methods).

Key Concepts

1
Each primitive has a corresponding wrapper class: Byte, Short, Integer, Long, Float, Double, Character, Boolean. Wrappers are objects (heap-allocated) and can be null — primitives cannot.
2
Default values: numeric primitives default to 0 (or 0.0), char to '\u0000', boolean to false. Wrapper classes default to null. This difference matters for instance fields — local variables have no default and must be explicitly initialized.
3
Wrapper classes are cached for small values: Integer caches -128 to 127 (IntegerCache). This means Integer.valueOf(127) == Integer.valueOf(127) is true, but Integer.valueOf(128) == Integer.valueOf(128) is false. This is a classic interview trap.
4
Size matters for collections: generics require objects, so List<int> is impossible — you must use List<Integer>. Project Valhalla aims to fix this with value types.