Reference Variable vs Object in Java
Fast recall
Quick revision
Core answer
null or a reference to an object; it is not the object itself and Java does not expose it as a raw address. Several variables can refer to the same object, so a mutation through one reference is visible through the others. == tests reference identity, while equals() tests the equality policy defined by the class.Whether you understand that a variable is not the object — it is a reference — and can reason about what happens when two variables point to the same object.
Treating `Student s1 = new Student()` as though `s1` IS the student. s1 is just the address. The real student data is on the heap.
Describe the two-variable-same-object scenario: `s2 = s1` means both variables point to the same heap object. Changing one changes what the other sees — there is no copy.
Interview-ready guide
Interview answer
A reference variable holds a reference to an object or null; it is not the object itself. If s2 = s1, Java copies the reference, so both variables refer to the same Student. A field change through one is then visible through the other, while reassigning one variable leaves the other unchanged.
== on references tests whether they denote the same object. equals() can define value equality. I avoid calling the reference a reference identity or pointer because Java exposes reference semantics, not portable address values or pointer arithmetic.
Reference Variable vs Object in Java
Direct Answer
A reference variable holds either null or a reference to an object; it is not the object itself and Java does not expose it as a raw address. Several variables can refer to the same object, so a mutation through one reference is visible through the others. == tests reference identity, while equals() tests the equality policy defined by the class.
Code Example
public class ReferenceDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Alice";
s1.marks = 95;
// s2 is a second reference to the SAME object — not a copy
Student s2 = s1;
s2.marks = 100; // modifying through s2...
System.out.println(s1.marks); // 100 — s1 sees the change too
// Proof: s1 and s2 point to the same heap object
// Null reference
Student s3 = null;
// s3.marks; // NullPointerException — no object behind this reference
// Comparing references with ==
System.out.println(s1 == s2); // true — same object
Student s4 = new Student(); // a brand new object
System.out.println(s1 == s4); // false — different object, different object
}
}
class Student {
String name;
int marks;
}
Common Mistakes & Pitfalls
- Treating the variable as the object —
s1is only the address; the real Student data lives on the heap, and saying "s1is the student" hides every bug that comes from sharing. - Expecting
s2 = s1to make a copy — it copies the reference, so both variables point at one object; modifying throughs2is instantly visible throughs1. - Comparing objects with
==—==checks whether two references hold the same object, not whether the objects have equal content; use.equals()for content, and two separatenewobjects are==false even with identical fields. - Calling a method on a
nullreference —s3.namewhens3isnullthrowsNullPointerException; a declared reference is not an object until something is assigned to it.
Check your understanding
Answer these without looking back at the article.
- What is a NullPointerException and how do you prevent it?
- What is the difference between == and .equals() for objects in Java?
- How do you create a deep copy of an object in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.