Understanding Java: Pass‑by‑Value vs Pass‑by‑Reference Explained
This article clarifies the long‑standing confusion about whether Java uses pass‑by‑value or pass‑by‑reference by defining the concepts, distinguishing actual and formal parameters, and demonstrating with primitive and object examples that Java always passes arguments by value, using the reference as the value for objects.
While browsing Stack Overflow, the author noticed a hugely popular question—"Is Java pass‑by‑value or pass‑by‑reference?"—with over 1.8 million views, indicating that many developers are puzzled by this topic.
1. Pass‑by‑Value and Pass‑by‑Reference
Pass‑by‑value means the argument is copied before being passed to the method, so changes to the parameter do not affect the original argument. Pass‑by‑reference means the argument’s address is passed directly, so modifications inside the method affect the caller’s variable.
The key differences are whether a copy of the argument is made and whether changes to the parameter affect the original argument.
2. Actual (real) and Formal (formal) Parameters
Formal parameters are the variables declared in a method signature; they receive the values supplied by the caller. Actual parameters are the values or variables supplied at the call site.
Example ("Hello World"):
public class Cmower {
public static void main(String[] args) {
System.out.println("hello world");
}
}Here args is the formal parameter, while the string "hello world" is the actual argument.
3. Primitive Types Are Passed by Value
Primitive types (e.g., int ) are copied when passed to a method. Changing the formal parameter inside the method does not affect the original variable.
public class Cmower {
public static void main(String[] args) {
Cmower cmower = new Cmower();
int age = 18;
cmower.sop(age);
System.out.println("main age " + age);
}
public void sop(int age) {
age = 28;
System.out.println("sop age " + age);
}
}Output:
sop age 28
main age 18The original age remains 18, confirming pass‑by‑value.
4. Reference Types Are Also Passed by Value (The Value Is the Reference)
When an object is passed, the value that is copied is the reference (memory address) to that object. The reference itself is not changed, but the object it points to can be mutated.
public class Cmower {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public static void main(String[] args) {
Cmower cmower = new Cmower();
cmower.setName("沉默王二");
cmower.sop(cmower);
System.out.println("main cmower " + cmower.getName());
}
public void sop(Cmower cmower) {
cmower.setName("沉默王三");
System.out.println("sop cmower " + cmower.getName());
}
}Output:
sop cmower 沉默王三
main cmower 沉默王三Even though the method changed the object's field, the original reference in main still points to the same object, so the change is visible.
Further demonstration shows that assigning the original reference to another variable ( old == cmower ) yields true , confirming that the reference value itself was passed unchanged.
5. Summary
Both primitive and reference types are passed by value in Java. For primitives, the value is the actual data; for objects, the value is the reference to the object. Consequently, modifications to an object’s fields inside a method are reflected outside the method because both callers share the same object via the copied reference.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.