All topics
library
beginner

Pass by Value in Java

Java is ALWAYS pass by value — understand what this means for primitives and objects.

Java always passes by value. There is no pass by reference. What gets copied for an object is the reference.

Like giving someone a copy of your house key (reference). They can go in and rearrange furniture (modify the object). But if they throw away their copy and get a new key (reassign the reference), your key still opens the same house.

Key Concepts

1
    void rename(Dog d) { d.name = "Rex"; }        // changes the caller's dog
2
    void replace(Dog d) { d = new Dog("Rex"); }   // changes nothing
3
Both look like they should work, and only the first does.
    Dog mine = new Dog("Bruno");
    rename(mine);     print(mine.name);    // "Rex"
    replace(mine);    print(mine.name);    // "Rex", unchanged by replace
4
Why. The parameter d is a copy of the reference. Both copies point at the same object, so d.name = reaches it. But d = new Dog(...) only repoints the copy, and the caller's variable still points where it did.
dd.name =d = new Dog(...)
5
For a primitive, the value itself is copied.
    void bump(int n) { n++; }
6
    int x = 1;
    bump(x);          // x is still 1
7
The classic failed swap.
    void swap(Dog a, Dog b) { Dog t = a; a = b; b = t; }   // does nothing
8
Only the local copies are exchanged. In a language with real pass by reference the caller's variables would be swapped; in Java you must return the values or pass a holder object.
9
Where the confusion comes from. People see rename change the object and conclude the reference was passed by reference. It was not — the reference was copied, and a copy of a pointer still points at the same thing.
rename
10
The C comparison settles it. Java behaves exactly like C passing a pointer by value: you can dereference it to modify the target, and reassigning it is local to the function.
11
A practical consequence. A method cannot give you back a different object through its parameters. If it needs to, it returns the new value — which is why immutable APIs return new instances rather than modifying their arguments.
12
What the interviewer is probing.1. "Is Java pass by value or pass by reference?" Probing: the classic, and whether you can defend the answer. Stalls: "Pass by reference for objects." Moves up: always pass by value — for an object the reference is copied, so the method can mutate the object but cannot repoint the caller's variable.
13
2. "Write a swap(a, b) method. Why can you not?" Probing: whether you can demonstrate the rule. Stalls: "Just assign them." Moves up: only the local copies exchange; the caller's variables are untouched, which is the proof it is not pass by reference.
14
3. "A method sets its parameter to a new object. What does the caller see?" Probing: the distinction between mutating and reassigning. Stalls: "The new object." Moves up: the original — reassignment only repoints the copy.
15
4. "How does this compare with C++?" Probing: whether you can place Java against a language that has both. Stalls: "The same." Moves up: Java matches C passing a pointer by value; C++ references genuinely alias the caller's variable, which Java has no equivalent of.