Is Java "pass-by-reference" or "pass-by-value"?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Java is strictly pass-by-value. The confusion comes from the fact that when you pass an object variable, the copied value is a reference to the object. That copied reference still points to the same object, so the method can mutate the object, but it cannot replace the caller's variable itself.
What Pass-by-Value Means in Java
When you call a method, Java copies the argument value into the parameter. The parameter is a new local variable inside the called method.
For primitives, this is easy to see:
a stays 10 because the method only modified its own local copy of the value.
Why Objects Cause Confusion
With objects, the copied value is a reference. Both the caller and the callee now hold references to the same object, so object mutation is visible through either reference.
This does not mean Java passed b by reference. It means Java copied the reference value, and both copies point to the same Box object.
Reassigning the Parameter Proves the Point
If Java were pass-by-reference, reassigning the parameter inside the method would change the caller's variable too. But it does not:
The method changed only its own local copy of the reference. The caller's variable b still points to the original object.
A Useful Mental Model
Think of it this way:
- primitives: Java copies the primitive value
- objects: Java copies the reference value
In both cases, the rule is the same: Java passes by value.
The only difference is what the value represents.
Why "Pass-by-Reference" Sounds Plausible
People often say Java "feels" pass-by-reference because mutations to objects are visible outside the method. That observation is real, but the explanation is not pass-by-reference semantics. The explanation is shared object identity through copied references.
This distinction matters when reasoning about APIs, immutability, defensive copying, and side effects.
Common Pitfalls
The biggest mistake is teaching "objects are passed by reference" as a shortcut. That creates confusion later when reassignment examples do not behave the way true pass-by-reference would imply.
Another issue is assuming methods can swap caller variables by reassigning parameters. They cannot. A swap method for object references does not work the way it would in a true pass-by-reference language.
Developers also sometimes overlook object mutation entirely and think pass-by-value means nothing can change outside the method. That is also false when the copied value is a reference to a mutable object.
Finally, when API clarity matters, immutable objects help reduce this confusion because methods cannot secretly mutate shared state through copied references.
Summary
- Java is always pass-by-value.
- For primitives, the copied value is the primitive itself.
- For objects, the copied value is the reference to the object.
- Methods can mutate a shared object through the copied reference, but they cannot replace the caller's variable.
- Reassigning a parameter does not prove pass-by-reference; it proves the opposite.
Related reading
- Is a HashMap thread-safe for different keys?
- Is a Java hashmap search really O1?
- Is a Java string really immutable?
- Is asynchronous jdbc call possible?
- Is asynchronous jdbc call possible?
- Is ExecutorService specifically ThreadPoolExecutor thread safe?
- Is it bad practice to make a setter return this?
- Is it bad to use polling in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.