skip to content

Explain the difference between a reference variable and the object it points to in Java.

level: juniorimportance: must knowfreq 80%

answer

  1. variable holds a handle, not the object
  2. object lives on heap, local var on stack
  3. aliasing: many refs, one object
  4. reassign repoints the label only
  5. null = points at nothing → NPE
  6. == identity vs equals() value

basics

~20 s

A reference variable is like a label that holds the address of an object; the object is the actual data living on the heap. Many labels can point to the same object, and a label can point to nothing (null).

solid answer

~40 s

A reference variable holds a *handle* (conceptually an address) to an object; it is not the object itself. The object — the real data with its fields and identity — lives on the heap. Several reference variables can point to the same object, so changing the object through one variable is visible through all of them (aliasing). Reassigning a variable only repoints that one label; it doesn't touch the object or other references. A reference can also be `null`, meaning it points to no object, and dereferencing it throws `NullPointerException`. `==` on references compares identity (same object?), whereas `equals()` compares logical value. This reference-vs-object split underlies how Java passes arguments: the reference value is copied, but both copies still point to the same shared object.

code

java · 9 lines
java
Point p1 = new Point(1, 2);
Point p2 = p1;          // same object, two references
p2.x = 99;
System.out.println(p1.x);        // 99  (aliasing: one shared object)
System.out.println(p1 == p2);    // true (identity: same object)

Point p3 = new Point(99, 2);
System.out.println(p1 == p3);    // false (different objects)
System.out.println(p1.equals(p3)); // depends on whether equals() is overridden

go deeper

for a junior

Knows the variable holds an address/handle and the object is the real data; can explain that two variables can point to the same object.

for a middle

Explains aliasing, reassignment vs mutation, null/NPE, and == vs equals() clearly with examples.

for a senior

Connects the reference/object split to pass-by-value semantics and identity, and reasons about defensive copying to avoid unintended aliasing.

for a principal

Discusses reference internals (compressed oops, GC moving objects and updating references), immutability as an aliasing-safety strategy, and API design that prevents reference leaks.

## Two separate things Beginners often blur "the variable" and "the object". They are two different things in two different places. - An **object** is the actual data structure created by `new`, living on the **heap** (the JVM's managed memory pool for objects). It has **fields** (its state), behavior (methods), and a unique **identity** (it is *this particular* object, distinct from any other even if their fields match). - A **reference variable** is a named slot that stores a **reference** — a handle, conceptually a memory address, that *points at* an object. The variable lives wherever it's declared: a **local variable** lives on the thread's **stack**; an **instance field** lives inside its owning object on the heap. So in `Dog d = new Dog();`, the `Dog` object is on the heap, and `d` is a slot holding its address. ## Consequences of the split **1. Aliasing — many references, one object.** ```java Dog a = new Dog(); Dog b = a; // copies the reference, NOT the object b.name = "Rex"; // mutates the one shared object System.out.println(a.name); // "Rex" — a and b point to the same Dog ``` There is *one* Dog and *two* handles to it. A mutation through either handle is visible through both. This is **aliasing**. **2. Reassignment repoints the label only.** ```java Dog a = new Dog(); Dog b = a; b = new Dog(); // b now points to a different, new Dog ``` Reassigning `b` changes only `b`'s slot; `a` and its object are untouched. **3. `null` — a reference to nothing.** A reference can hold the special value `null`, meaning "points at no object". Calling a method or accessing a field through a `null` reference throws **`NullPointerException`** (NPE), because there is no object to act on. **4. Identity vs. equality.** `==` between two references asks *"are these the same object?"* (identity). `equals()` asks *"are these logically equal?"* (value), if the class overrides it. Two distinct objects with identical fields are `==`-unequal but may be `equals()`-equal. **5. Parameter passing.** Java is strictly **pass-by-value**, but for objects the *value passed is the reference*. The method gets its own copy of the handle pointing at the same object — so it can mutate the object (visible to the caller) but reassigning the parameter doesn't change the caller's variable. ## Why Java hides the address Unlike C/C++ pointers, you cannot do arithmetic on a Java reference or read its numeric address. The JVM may even move objects in memory during garbage collection and silently update references. You only get "points at this object" or "points at nothing" — which removes whole classes of pointer bugs.

  • If `Dog b = a;` and you call `b.setName("Rex")`, what does `a` see, and why?
    `a` sees the name "Rex". `b = a` copied the reference, so both point to one shared Dog; mutating through `b` mutates the same object `a` points to.
  • Is Java pass-by-reference because mutating an object inside a method is visible to the caller?
    No. Java is pass-by-value; the value copied is the reference. Mutation is visible because both copies point to the same object, but reassigning the parameter does not affect the caller's variable.

A reference is a contact saved on your phone (a number pointing to a person); the object is the actual person. You and a friend can both save the same number (aliasing) — if that person moves house, both contacts still reach them. Deleting your contact (reassign/null) doesn't delete the person.

saying these in an interview costs you the question

  • Saying Java is pass-by-reference for objects
  • Believing `b = a` copies the object (deep or shallow)
  • Thinking `==` compares contents for objects
  • Assuming a reference exposes a usable numeric address like a C pointer

context