Course contentsShow
← Java
Lesson 274 of 2,8707. Reference Types and Object FundamentalsFree lesson

Primitives vs Reference Types: Fundamental Differences

Learn how primitives store values directly while reference types store memory addresses pointing to objects.

Primitives vs Reference Types: Fundamental Differences

What you'll learn: You'll understand the core distinction between how primitives and reference types store data in memory.

The Core Difference

In Java, primitives store actual values directly, while reference types store memory addresses that point to where objects live.

Think of it like this: A primitive is like writing someone's phone number directly on a sticky note. The note contains the number itself. A reference type is like writing "in the phone book, page 47" — the sticky note doesn't contain the phone number, just directions to find it elsewhere.

Primitives: Direct Storage

When you declare a primitive variable like int age = 25;, the variable age holds the value 25 directly in its memory location. The value is the data.

int x = 10;
int y = x;  // Copies the value 10 to y
x = 20;     // Changing x doesn't affect y
// x is now 20, y is still 10

Reference Types: Indirect Storage

When you declare a reference type like an array, the variable doesn't hold the array data itself — it holds an address (a reference) pointing to where the array exists in memory.

int[] arr1 = new int[]{1, 2, 3};
int[] arr2 = arr1;  // Copies the address, not the array!
arr1[0] = 99;       // Changes the array that arr1 points to
// arr2[0] is also 99 — both variables point to the same array

Both arr1 and arr2 reference the same memory location, so modifying through one affects what you see through the other.

Why This Matters

This fundamental difference explains why copying primitives creates independent values, but copying references creates multiple "windows" into the same object.

Key Takeaway: Primitives hold values directly; reference types hold addresses pointing to objects in memory, which means multiple references can point to the same object.