Call by Value
Call by Value means that Java passes a copy of the value to a method, not the original variable itself. Any changes made to the parameter inside the method do not affect the original variable in the caller. This is a frequently asked interview topic, especially to clarify the confusion around objects and references.
Is Java Call by Value or Call by Reference?
Java is strictly CALL BY VALUE. Always.
- For primitives → copy of the value is passed
- For objects → copy of the reference is passed
- The original reference itself is never passed
Call by Value with Primitive Types
Example
void change(int x) {
x = 50;
}
public static void main(String[] args) {
int a = 10;
change(a);
System.out.println(a);
}
Output:
10
Explanation
- a = 10
- Copy of a is passed to x
- Changes to x do NOT affect a
Call by Value with Object References (Very Important)
Example
void change(int[] arr) {
arr[0] = 100;
}
public static void main(String[] args) {
int[] a = {10, 20};
change(a);
System.out.println(a[0]);
}
Output:
100
Explanation
- Copy of reference a is passed
- Both references point to the same object
- Object content is modified
- Reference itself is not changed
Why This Is Still Call by Value
void change(int[] arr) {
arr = new int[]{50, 60};
}
int[] a = {10, 20};
change(a);
System.out.println(a[0]); // 10
Explanation
- Reference copy is reassigned inside method
- Original reference a remains unchanged
- Confirms Java is not call by reference
Visual Understanding (Conceptual)
Primitive
a = 10
copy -> x = 10
Object
a ----> [10, 20]
copy of reference ----> [10, 20]
Both references point to same object, but reference itself is copied.
Common Misconception
- ❌ Java supports call by reference
- ❌ Objects are passed by reference
- ✔ Java passes everything by value
- ✔ For objects, the value is the reference
Call by Value vs Call by Reference (Conceptual)
| Feature | Call by Value (Java) | Call by Reference |
|---|---|---|
| What is passed | Copy of value | Original reference |
| Affects caller variable | ❌ No | ✅ Yes |
| Java supports | ✅ Yes | ❌ No |
Why Java Uses Call by Value
- Simpler and safer
- Avoids unintended side effects
- Improves security and predictability
- Easier memory management
Common Beginner Mistakes
- Expecting primitive values to change
- Thinking Java is call by reference
- Confusing object mutation with reference change
- Not understanding reference copying
Interview-Ready Answers
Short Answer
Java uses call by value, meaning a copy of the value is passed to methods.
Detailed Answer
In Java, all method arguments are passed by value. For primitive types, a copy of the actual value is passed. For objects, a copy of the reference is passed, allowing object modification but not reference reassignment. Java does not support call by reference.
Key Takeaway
Java is 100% call by value. Understanding how primitive values and object references are passed prevents bugs and clears one of the most common interview traps.