Is Java pass-by-value or pass-by-reference? Prove it.
⚡ Short Answer
Java is always pass-by-value. For objects, the value passed is a copy of the reference — so you can mutate the pointed-to object, but reassigning the parameter doesn't affect the caller's variable.
☕Coffee Chat Question
Concept Made Simple
“Is Java pass-by-value or pass-by-reference? Prove it.”
🧠Mind Map Answer
Remember It Faster
The method gets a copy of the reference. Following it lets you mutate shared state; reassigning the copy is local only.
⌨️Hands-on Keyboard
Learn by Doing
void tweak(StringBuilder sb) {
sb.append("!"); // mutates shared object → visible
sb = new StringBuilder("x"); // reassign copy → invisible
}
StringBuilder s = new StringBuilder("hi");
tweak(s);
System.out.println(s); // hi!hi!
🔥What If?
Think Beyond the Expected
A method 'swaps' two object parameters but the caller sees no change — why?
Swapping only exchanges the local reference copies inside the method. The caller's variables still point to the original objects. Java can't swap caller references — that would require pass-by-reference.
😂Real World
A common bug: passing a list to a method expecting it to 'replace' the list (param = newList) — it doesn't; you must mutate (list.clear()/addAll) or return the new list.
🎯Interviewer's Expectation
Keywords they're listening for:
⚠️Common Mistakes
- ✗Believing objects are pass-by-reference
- ✗Expecting param reassignment to affect the caller
- ✗Mutating a shared collection passed by another layer
✅Best Practices
- ✓Return new values instead of mutating params
- ✓Prefer immutable arguments
- ✓Defensively copy collections crossing API boundaries
🔁Follow-up Questions
- 1How would you return multiple values without out-params?
- 2Why are immutable params safer in concurrent code?
- 3How does this differ from C++ references?
🧩Related Technologies
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: OOP (Core Java) Interview question: "Is Java pass-by-value or pass-by-reference? Prove it." Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
⭐ Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.