Reference types
A reference type variable holds the address of an object on the heap, not the object itself. Classes, interfaces, arrays, enums, and records are all reference types.
Detailed Explanation
Java has exactly two kinds of value. A primitive variable holds a number directly. A reference variable holds a handle that points at an object stored elsewhere in memory. Everything that is not one of the eight primitives is a reference type.
The indirection buys you three things. Objects can be large and variable in size without copying costs when passed around. Two variables can refer to the same object, which is how shared mutable state and object graphs work at all. And a reference can be absent, represented by null, which primitives cannot express.
The mental model that matters most: assignment copies the reference, not the object. If you have a ShoppingCart and write var backup = cart;, you now have two variables pointing at one cart. Adding an item through backup changes what cart sees, because there is only one cart. Learners who expect a copy write bugs here constantly.
The same rule explains Java's calling convention. Java is always pass-by-value, with no exceptions, but the value being passed for a reference type is the reference. A method can mutate the object you handed it, because it points at the same object. A method cannot make your variable point somewhere else, because it received a copy of the reference. Reassigning a parameter inside a method is invisible to the caller.
Equality follows from this too. == compares references: are these two variables pointing at the same object? equals compares content, if the class has overridden it to do so. For String and boxed numbers, == sometimes appears to work because of pooling and caching, which makes the bug intermittent rather than obvious. Use equals for value comparison always.
Reference types have a default of null in fields and array elements. Dereferencing null throws NullPointerException, historically Java's most common runtime failure. Java 14 added helpful messages that name the exact expression that was null, which turns a guessing game into a one-line diagnosis. Optional, added in Java 8, is the type-level answer for return values that may legitimately be absent.
Memory behaviour differs from primitives. Objects live on the heap, are allocated by the JVM, and are reclaimed by the garbage collector once no live reference reaches them. An object header of 12 to 16 bytes precedes every instance, so a Long costs roughly four times what a long field costs, and a Long[] of a million elements costs far more than a long[] of the same length because it stores a million pointers to a million separate objects.
Reach for reference types whenever you need identity, behaviour, absence, or a type that participates in generics. Reach for primitives for arithmetic and for large numeric arrays.
The costs are allocation pressure, pointer chasing that hurts cache locality, and the possibility of null. Records and immutable classes reduce the aliasing danger by removing mutation, and are the cheapest way to avoid the "two variables, one object" class of bug.
Code Examples
import java.util.ArrayList;
import java.util.List;
public class ReferenceSemantics {
static void addItem(List<String> cart) {
cart.add("keyboard"); // mutates the shared object: caller sees this
cart = new ArrayList<>(); // rebinds the local copy only: caller sees nothing
cart.add("ignored");
}
public static void main(String[] args) {
List<String> cart = new ArrayList<>();
cart.add("mouse");
List<String> backup = cart; // copies the reference, not the list
addItem(cart);
System.out.println(cart); // prints: [mouse, keyboard]
System.out.println(backup); // prints: [mouse, keyboard] — same object
System.out.println(cart == backup); // prints: true
}
}public class IdentityVsEquality {
record UserId(String value) { } // records get equals/hashCode generated
public static void main(String[] args) {
String a = new String("session-42");
String b = "session-42";
System.out.println(a == b); // prints: false — different objects
System.out.println(a.equals(b)); // prints: true — same characters
UserId first = new UserId("u-1");
UserId second = new UserId("u-1");
System.out.println(first == second); // prints: false
System.out.println(first.equals(second)); // prints: true
String missing = null;
// System.out.println(missing.length());
// would throw: NullPointerException: Cannot invoke "String.length()"
// because "missing" is null (helpful message, Java 14+)
System.out.println(missing == null); // prints: true — == with null is always safe
}
}Common Gotchas
`==` on objects almost never means what you want
it asks whether two references point at the same instance. Small integers and string literals are cached, so `==` can return `true` in tests and `false` in production with larger values.
`NullPointerException` on an uninitialised field
object fields default to `null`, so calling a method on one before the constructor assigns it fails at runtime. Prefer final fields assigned in the constructor.
Storing a mutable object you received keeps the caller's object
a constructor that saves the caller's `List` directly lets the caller mutate your internals later. Copy defensively with `List.copyOf` when the class is meant to be immutable.
Arrays are reference types too
`int[]` is an object even though its elements are primitives, so it has a header, lives on the heap, and `arr1 == arr2` compares identity rather than contents. Use `Arrays.equals` for contents.