Skip to content
Codescope

Primitives & wrappers

Eight types that hold values, and their object counterparts.

intro7 min5 steps

Before you start

Java's eight **primitive** types hold their value directly — an int variable literally contains the number 42, no indirection. Fast, cheap, but not objects.

Sometimes you need the same value *as* an object — for example, generic containers like List<T> insist on objects. That's why every primitive has a **wrapper** class: int→Integer, boolean→Boolean, and so on. The wrapper is a heap object that carries the value.

This lesson shows a bare int in its stack slot next to a boxed Integer on the heap — same number, radically different storage.

Stack

primitives hold their value directly
main()
ageint
25
4 bytes

No object. The value 25 is the entire content of the slot.

Step 1 of 5. int: the value is in the slot. `int age = 25` puts the number 25 directly in the variable's stack slot. There is no object, no allocation, no heap involvement. This is what makes primitives fast — reading `age` is a single memory access, not a pointer dereference.

01 / 05

Step 01 / 05

int: the value is in the slot

int age = 25 puts the number 25 directly in the variable's stack slot. There is no object, no allocation, no heap involvement.

This is what makes primitives fast — reading age is a single memory access, not a pointer dereference.

primitives-and-wrappers.javaJava
public class Main {    public static void main(String[] args) {        // Primitives — value in the slot        int    age   = 25;        double price = 9.99;        boolean ok   = true;        char   grade = 'A';        // Wrapper — object on the heap        Integer boxed = Integer.valueOf(25);        // The eight primitives and their wrappers        // byte  → Byte      (1 byte)        // short → Short     (2 bytes)        // int   → Integer   (4 bytes)        // long  → Long      (8 bytes)        // float → Float     (4 bytes)        // double→ Double    (8 bytes)        // char  → Character (2 bytes)        // boolean→ Boolean  (1 bit, stored as byte)        // Why wrappers exist: generics need objects        List<Integer> nums = new ArrayList<>();        nums.add(42);   // auto-boxed: int → Integer    }}