MediumExperience level: 0-2 years · 3-5 years1 min read

Is Java pass-by-value or pass-by-reference? Prove it.

Reviewed by Gurusankar M.

Asked inInfosysTCSCognizantWipro
#pass by value#references#method arguments#mutability
Report issue

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.

On this page

Concept Made Simple

Coffee Chat Question

“Is Java pass-by-value or pass-by-reference? Prove it.”

Remember It Faster

Mind Map Answer

The method gets a copy of the reference. Following it lets you mutate shared state; reassigning the copy is local only.

obj.setX(9)
caller sees it — same object
obj = new()
caller does NOT see it — local copy

Learn by Doing

Hands-on Keyboard

java
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!
Output
hi!

Think Beyond the Expected

What If?

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:

  • always pass-by-value
  • copy of the reference
  • mutate vs reassign
  • no reference swapping

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

  1. How would you return multiple values without out-params?
  2. Why are immutable params safer in concurrent code?
  3. How does this differ from C++ references?

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.