Java Reference ยท Types and Literals
Reference types
Reference for Java reference types: how object variables hold references, identity versus equality, aliasing, pass-by-value semantics and weak references.
Every type in Java is either a primitive (int, double, boolean and the other five) or a reference type: classes, interfaces, enums, records, arrays and type variables. A variable of reference type does not hold the object itself. It holds a reference, a handle that either points to an object on the heap or is null.
This has three consequences. Assigning one reference variable to another makes both point to the same object (aliasing). == compares the references, not the contents. And when you pass an object to a method, Java copies the reference, so the method can modify the object but cannot make the caller's variable point somewhere else.
StringBuilder a = new StringBuilder("hi"); StringBuilder b = a; // same object b.append("!"); // a also shows "hi!"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
ClassName variable; // null until assigned ClassName variable = new ClassName(args); InterfaceName variable = new Implementation();
| Concept | Behavior |
|---|---|
| Default value | null for fields and array elements; local variables must be assigned before use. |
== / != |
Compare references (identity). |
equals() |
Compares content if the class overrides it (strings, collections, records do). |
| Assignment | Copies the reference, not the object. |
| Method argument | The reference is passed by value. |
| Size | A reference is typically 4 or 8 bytes (compressed oops are the default on heaps under about 32 GB). |
Usage notes
- Compare with
equals()for content; use==only for primitives, enums, and deliberate identity checks. UseObjects.equals(a, b)when either side can be null. - Java is always pass-by-value. For objects the value is the reference. Reassigning a parameter never affects the caller; mutating the object does.
- Defensive copies. If a class stores or returns a mutable object, copy it so callers cannot change your internal state (
List.copyOf(),clone()). - Garbage collection. An object becomes eligible for collection when no live reference reaches it.
java.lang.refoffersWeakReference,SoftReferenceandCleanerfor caches and cleanup; most code never needs them. - Generics work only with reference types, which is why primitives are boxed.
finalapplies to the variable, not the object:final List<String> lcannot be reassigned but can still be modified.- Reference types can be cast between related types; a wrong cast throws
ClassCastException.