Java Is Always Pass-by-Value
Fast recall
Quick revision
Core answer
Whether you understand Java's evaluation strategy and can explain why modifying an object inside a method is visible to the caller while reassigning is not
Saying Java is pass-by-reference for objects because you can modify the object's fields inside a method
Demonstrate with a swap method that fails, and explain that the reference itself is copied — like having two remotes pointing at the same TV
Interview-ready guide
Interview answer
Java is always pass-by-value — no exceptions. The confusion is about what's being copied.
For primitives — straightforward
doSomething(x)passes a copy of the value; changes to the parameter inside the method have zero effect on the caller's variable
For objects — copies the reference, not the object
- Java passes a copy of the pointer to the heap object
sb.append(" World")inside a method IS visible to the caller — both references point to the sameStringBuilderon the heapsb = new StringBuilder("Replaced")inside a method is NOT visible to the caller — it only redirects the local copy of the reference
The definitive proof
- Write a
swap(String a, String b)method — the caller's variables never move - In C++ with
&references, the swap works because you get the actual variable slots - In Java, you get copies of the pointers; redirecting copies doesn't affect the originals
I recommend using the "copy of the house key" mental model — you can go inside and rearrange furniture (mutation), but melting your key doesn't change the original.
Java Is Always Pass-by-Value
Direct Answer
Java is always pass-by-value. For primitives, it passes a copy of the value. For objects, it passes a copy of the reference — not the object itself. You can modify the object through the copied reference, but reassigning the reference inside the method has no effect on the caller.
Deep Dive & Architecture
Varargs (`Type...
args`) = syntactic sugar — compiler converts to array.
Must be last parameter, at most one per method. Can pass 0+ args or an explicit array. Heap pollution with generics (T... args) — @SafeVarargs when provably safe. Null trap: method(null) passes as null array — args.length throws NPE. Avoid combining varargs with overloading — compiler resolution can produce surprises.
Saying Java is pass-by-reference for objects because you can modify the object's fields inside a method
Common Mistakes & Pitfalls
Saying Java is pass-by-reference for objects because you can modify the object's fields inside a method
Check your understanding
Answer these without looking back at the article.
- Varargs in Java — Variable Arguments and Heap Pollution
- What is autoboxing and unboxing in Java? What is the Integer cache trap?
- How does HashMap work internally in Java?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.