← Blog
Android · Java · Interview Prep

Java Always Passes by Value — Not by Reference!

✍️ Ahmed Elmenshawie
April 2026
⏱️ 6 min read
Java pass by value vs pass by reference

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.

🔑 The Golden Rule

Java is always pass by value. Always. No exceptions. The confusion arises because for objects, the "value" being passed is a reference (memory address) — but the reference itself is passed by value.

📦 Primitives — Crystal Clear

For primitive types like int, boolean, double, this is completely straightforward. Java copies the actual value — the method gets its own isolated copy:

Java
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.

🧩 Objects — Where People Get Confused

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:

Java
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" ✅

🧠 Why Does changeName() Work?

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.

❌ Why Does reassignPerson() NOT Work?

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.

🎯 Interactive Memory Diagram

The Verdict
Java passes a copy of the reference for objects, and a copy of the value for primitives. Either way — it's always pass by value. Say this confidently in your next interview.

🧪 Test Yourself

🧠

Quick Knowledge Check

3 questions — no pressure, just learning

Question 1 of 3 Score: 0

🚀 Interview Tip

When an interviewer asks about pass-by-value vs pass-by-reference in Java, say this:

"Java is strictly pass-by-value. For primitives, the value itself is copied. For objects, the reference (memory address) is copied — so you can modify the object's state through the copied reference, but you cannot reassign the original reference."

That answer alone will immediately distinguish you from 80% of candidates who get this wrong. Now go ace that interview! 💪

Ahmed Elmenshawie
Ahmed Elmenshawie
Android Developer, Tech Mentor, and bridge between Cairo and Berlin. I write about Android, Java, career growth, and studying in Germany.