In this article, we’ll answer the widely debated question: Is Java pass-by-value or pass-by-reference? The short answer is: Java is strictly pass-by-value. This holds true even when handling objects, though Java’s approach to object references often creates confusion. Let’s explore this concept in depth, with examples to clarify how Java’s pass-by-value mechanism works for both primitive types and objects.
Understanding Java’s Pass-by-Value Mechanism
Java always uses pass-by-value when passing variables to methods. This means that Java passes a copy of the variable’s value, not the variable itself. Let’s break down how this works with both primitive types and object references.
Pass-by-Value with Primitives
When you pass a primitive type (such as int
or char
) to a method, Java makes a copy of the value. The method works with this copy, meaning changes within the method do not affect the original variable.
public class PassByValueExample {
public static void main(String[] args) {
int number = 5;
modifyPrimitive(number);
System.out.println("After method call, number is: " + number); // Output: 5
}
static void modifyPrimitive(int num) {
num = 10;
}
}
Explanation: In this example, modifyPrimitive receives a copy of number. Changes to num inside the method do not affect number in the main method, so the output remains 5.
Is Java Pass-by-Value or Reference When It Comes to Object References?
For objects, Java also uses pass-by-value, but here’s the key difference: the value passed is a copy of the object’s reference (i.e., the memory address where the object is stored), not the object itself. Thus, modifications to the object’s fields inside a method affect the original object. However, reassigning the reference within the method won’t impact the original reference.
public class PassByValueExample {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.value = 5;
modifyObject(obj);
System.out.println("After method call, obj.value is: " + obj.value); // Output: 10
}
static void modifyObject(MyObject obj) {
obj.value = 10;
}
}
class MyObject {
int value;
}
Explanation: Here, modifyObject can change obj.value because the reference (memory address) points to the same object. Thus, the change to value affects the original object.
Reassigning Object References in Java: A Test for Pass-by-Value or Reference?
If a method reassigns an object reference, it only affects the local copy within the method, leaving the original reference outside the method unaffected.
public class PassByValueExample {
public static void main(String[] args) {
MyObject obj = new MyObject();
obj.value = 5;
reassignObject(obj);
System.out.println("After method call, obj.value is: " + obj.value); // Output: 5
}
static void reassignObject(MyObject obj) {
obj = new MyObject();
obj.value = 10;
}
}
class MyObject {
int value;
}
Explanation: In reassignObject
, the reference obj
is reassigned to a new MyObject
inside the method. This new assignment does not affect the original obj
in main
, so obj.value
remains 5
after the method call.
Conclusion
To summarize, Java passes parameters by value. For primitive types, this means a copy of the value is passed. For objects, this means a copy of the reference is passed, allowing methods to modify the object’s fields but not the reference itself.