HuntExams Academy logo
JavaIntermediate#fundamentals

What is the difference between pass-by-value and pass-by-reference in Java?

Answer

Java is always pass-by-value. For objects, the value passed is a copy of the reference, so mutating the object's state through it affects the original, but reassigning the parameter does not affect the caller's reference.

Example

void modify(StringBuilder sb) { sb.append("x"); sb = new StringBuilder("new"); }
StringBuilder s = new StringBuilder("a");
modify(s);
System.out.println(s); // "ax" — append worked, reassignment didn't

Detailed explanation

Java is always pass-by-value, for both primitive values and object references. Pass-by-value means a method parameter receives a copy of the value supplied by the caller. Pass-by-reference, in contrast, would let the parameter act as an alias for the caller’s variable, allowing the method to replace what that variable holds. Java does not provide this parameter-passing mechanism. The important distinction is between changing an object and changing the variable that refers to it; these are separate operations with different effects on the caller.

For primitive types such as int, boolean, and double, the copied value is the primitive value itself. If a caller has int n = 10 and passes n to a method, assigning 20 to the parameter does not change n. For a reference type, the copied value is a reference to an object, not a copy of the object. The caller’s variable and the method’s parameter therefore initially refer to the same object. Accessing that object through either reference can reveal changes to its mutable state.

In the example, s refers to a StringBuilder containing "a". Calling modify(s) copies that reference into the parameter sb. The statement sb.append("x") modifies the shared StringBuilder, so its contents become "ax". Next, sb = new StringBuilder("new") replaces only the reference stored in the parameter. It does not replace the reference stored in s. Consequently, printing s after the call produces "ax". At the JVM execution level, argument values initialise the called method’s parameter slots; assigning to a parameter updates its own slot, not the caller’s variable.

Interviewers commonly test this distinction using object mutation, parameter reassignment, and swap methods. A method that merely exchanges two object-reference parameters cannot swap the caller’s variables. Likewise, passing an array permits changes to its elements, but assigning a new array to the parameter does not replace the caller’s array reference. Immutable objects such as String follow exactly the same passing rule; they simply do not support state mutation. To let the caller adopt a replacement object, return it and have the caller assign the result.

Key points to remember

  • Java always passes arguments by value; it never passes variables by reference.
  • A primitive argument supplies a copy of its primitive value.
  • An object argument supplies a copy of its reference, not a copy of the object.
  • Mutating a shared object can affect what the caller observes.
  • Reassigning a parameter, including assigning null, does not reassign the caller’s variable.

Common mistakes candidates make

  • Saying that primitives are passed by value but objects are passed by reference.
  • Assuming pass-by-value creates an independent copy of each object.
  • Expecting parameter reassignment or a simple reference-swapping method to update the caller’s variables.

Likely follow-up questions

Why does changing a String parameter not change the caller’s String?

String is immutable, so operations such as concatenation do not modify the existing String object. Assigning the result to the parameter only changes that parameter’s copied reference.

Can a Java method swap two objects?

A method cannot swap the caller’s reference variables by exchanging its parameters. It can swap elements inside a shared array or mutable holder, or return values for the caller to assign.

Is passing an object reference the same as making a shallow copy?

No: passing a reference does not create another object at all. A shallow copy creates a separate outer object but typically copies its reference fields, leaving referenced nested objects shared.

Interview tip: State the rule first, then trace the StringBuilder example to contrast shared-object mutation with local parameter reassignment.

Related Interview Questions

1
JavaBeginner#fundamentals

What is the difference between static and instance methods?

Open
2
JavaBeginner#oop

What is a constructor and how does constructor overloading work?

Open
3
JavaIntermediate#oop

What is the difference between instance initializer blocks and constructors?

Open