If you've been preparing for Android or Java interviews, you've probably heard someone confidently say: "Java passes objects by reference." Spoiler alert: they're wrong — and if you say that in an interview, it will raise a red flag.
Let's break this down once and for all so you can walk into your next interview knowing the truth.
For primitive types like int, boolean, double, this is completely straightforward. Java copies the actual value — the method gets its own isolated copy:
void increment(int x) { x = x + 1; // Only modifies the LOCAL copy } int num = 5; increment(num); System.out.println(num); // Still 5 ✅
No surprise here — num is unchanged because Java passed a copy of the integer value. The method's x is a completely separate variable.
Here's where it gets tricky. Let's say you have a Person object. Two methods — one that modifies the object's fields, and one that tries to reassign the reference entirely:
class Person { String name; Person(String name) { this.name = name; } } void changeName(Person p) { p.name = "Berlin"; // ✅ Modifies the original object } void reassignPerson(Person p) { p = new Person("Cairo"); // ❌ Only changes the local copy } Person ahmed = new Person("Ahmed"); changeName(ahmed); System.out.println(ahmed.name); // "Berlin" ✅ reassignPerson(ahmed); System.out.println(ahmed.name); // Still "Berlin" — NOT "Cairo" ✅
When you call changeName(ahmed), Java copies the memory address of the ahmed object and passes that copy to the method. Both ahmed and p now point to the same object in heap memory.
So when you do p.name = "Berlin", you're modifying the actual object that both variables point to. The mutation travels through the copied address. That's why it works.
When you do p = new Person("Cairo") inside the method, you're only changing the local copy of the reference. The original ahmed variable still points to the old object in memory.
This is the clearest proof that Java passes references by value, not by reference. If it were true pass-by-reference, reassignPerson would have changed what ahmed points to — but it didn't.
3 questions — no pressure, just learning
When an interviewer asks about pass-by-value vs pass-by-reference in Java, say this:
That answer alone will immediately distinguish you from 80% of candidates who get this wrong. Now go ace that interview! 💪