References, aliasing & null in Java
Two variables can point to one object; NullPointerException and helpful NPE messages.
Variables hold addresses
An object is a house; a variable is a slip of paper with its address. Box b = a; copies the address, not the house. Now two slips lead to one house — that's called *aliasing*.
Box a = new Box();
Box b = a; // same house, two slipsPaint through the alias
What does this print?
class Box { int v; }
void main() {
Box a = new Box();
Box b = a;
b.v = 7;
System.out.println(a.v);
}07null
Show the answer
7. a and b are aliases for the same Box, so a change made through b is visible through a.
Passing to methods
Java passes a copy of the reference (pass by value). The method gets its own slip with the same address: it can change the house, but if it writes a new address on its slip, the caller's slip is unaffected.
Two moves in one method
What does this print?
class Box { int v; }
void paint(Box b) {
b.v = 1;
b = new Box();
b.v = 2;
}
void main() {
Box a = new Box(); paint(a);
System.out.println(a.v);
}012
Show the answer
1. The first line changed the shared object. Then b was pointed at a brand-new Box — the caller's a never saw that, so the 2 went to an object nobody keeps.
null: a blank slip
A reference can be **null** — a slip with no address. Reference-type fields start as null. Using a null reference (calling a method, reading a field) throws **NullPointerException**.
The helpful NPE
name was never assigned, so it's null, and .length() crashes. Since Java 14 (on by default from Java 15, JEP 358), the message names the culprit: *Cannot invoke "String.length()" because "u.name" is null*.
class User { String name; }
User u = new User();
u.name.length(); // NPEIn real projects
Aliasing bugs are sneaky: two parts of an app share one list, and one "harmlessly" clears it. NullPointerException is the most common Java exception in production — helpful NPE messages turned many hour-long hunts into a glance at the log.
Key takeaways
- Assignment copies the reference, not the object
- Aliases see each other's changes
- Java passes references by value to methods
- Helpful NPE messages name the null expression
💡 A reference is a house address on a note. Copy the note and both notes lead to the same house.
On 64-bit JVMs, references are often squeezed into 32 bits ("compressed oops"), turned on by default for heaps under about 32 GB — saving lots of memory on every reference.
Practice questions
What does this print?
class Box { int v; }
void main() {
Box a = new Box();
Box b = a;
b.v = 9;
System.out.println(a.v);
}- 0
- 9
- null
- Compile error
Check your answer
9. a and b are aliases for the same Box, so changing the object through b is visible through a.
What does this print?
class Box { int v; }
void reset(Box b) {
b = new Box(); b.v = 1;
}
void main() {
Box a = new Box();
a.v = 5;
reset(a);
System.out.println(a.v);
}- 1
- 5
- 0
- null
Check your answer
5. Java passes a copy of the reference. reset points its own copy at a new Box; the caller's a still points to the original, untouched object.