What is the difference between primitive types and reference types in Java?

6 minbeginnerprimitivestypesbasics

Quick Answer

Primitives (byte, short, int, long, float, double, char, boolean) hold actual values directly, live on the stack (or inline in objects), and have fixed default values. Reference types (classes, interfaces, arrays) hold a reference/pointer to an object on the heap; the variable itself can be null.

Detailed Answer

Java has exactly 8 primitive types: byte, short, int, long, float, double, char, boolean. A primitive variable stores its value directly (in a stack frame, or inline within an object/array) — there's no separate object, no header, no reference indirection.

Everything else — classes, interfaces, arrays, enums — is a reference type. A reference-type variable stores a pointer to an object allocated on the heap; multiple variables can reference the same object, and the variable itself may be null.

int x = 5;              // value lives directly in x
Integer boxed = 5;      // x is a reference to a heap-allocated Integer object

int[] arr = {1, 2, 3};  // arr is a reference to a heap array object

Key practical differences:

  • Defaults: numeric primitives default to 0/0.0, boolean to false, char to ' '; references default to null.
  • Equality: == compares primitive values directly, but compares references (identity) for objects — use .equals() for value equality on objects.
  • Passing to methods: both are passed by value, but for a reference type that "value" is the reference itself, so mutating the referenced object is visible to the caller, while reassigning the parameter is not.

Related Resources