The Rule: Java is Pass-by-Value
The Fundamental Rule
Java is ALWAYS pass-by-value. There is no pass-by-reference in Java.
- Primitives: The actual value is copied
- References: The reference (address) is copied, not the object
This is the #1 Java misconception. Many developers believe Java passes objects by reference, but it doesn't.
public class PassByValueRule {
public static void main(String[] args) {
int x = 10;
changePrimitive(x);
System.out.println(x); // 10 (unchanged)
int[] arr = {1, 2, 3};
changeArray(arr);
System.out.println(arr[0]); // 999 (changed!)
}
static void changePrimitive(int val) {
val = 999; // Changes local copy only
}
static void changeArray(int[] array) {
array[0] = 999; // Changes the object via copied reference
}
}
Key insight: When you pass a reference, Java copies the reference. The method gets a copy of the reference, pointing to the same object.
Passing Primitives
Passing Primitives
When you pass a primitive, the value is copied. The method works with a local copy.
public class PrimitiveDemo {
public static void main(String[] args) {
int a = 5;
double b = 3.14;
boolean c = true;
increment(a);
System.out.println(a); // Still 5!
modify(b, c);
System.out.println(b); // Still 3.14!
System.out.println(c); // Still true!
}
static void increment(int x) {
x++; // Only changes local copy
}
static void modify(double d, boolean flag) {
d = 100.0;
flag = false;
// Changes are lost when method returns
}
}
Why primitives can't be modified:
- The method receives a copy on the stack
- Changes affect only the local stack frame
- Original value remains untouched
- No way to modify the caller's variable
Passing References
Passing References
When you pass an object, the reference is copied. The method gets a copy of the reference, pointing to the same object.
public class ReferenceDemo {
public static void main(String[] args) {
Person p = new Person("Alice");
// Reference 'p' is copied to changeName()
changeName(p);
System.out.println(p.name); // "Bob" (changed!)
// But we can't change what 'p' points to
resetPerson(p);
System.out.println(p.name); // Still "Bob"
}
static void changeName(Person person) {
// person is a COPY of 'p', but same object
person.name = "Bob"; // Modifies the object
}
static void resetPerson(Person person) {
person = new Person("Charlie"); // Only changes local copy
// 'p' in main still points to original object
}
}
Two things you can do with a reference:
- Modify the object (via the copied reference) ✓
- Change what the reference points to ✗ (only affects local copy)
The 'Pass by Reference' Myth
The 'Pass by Reference' Misconception
Many developers think Java passes objects by reference because modifications to objects persist. This is wrong.
public class MisconceptionDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
// This WORKS - looks like pass by reference
modifyArray(arr);
System.out.println(arr[0]); // 999
// But THIS doesn't - proves it's pass by value
int[] newArr = {4, 5, 6};
replaceArray(newArr);
System.out.println(newArr[0]); // Still 4, not 7
}
static void modifyArray(int[] array) {
array[0] = 999; // Modifies via copied reference
}
static void replaceArray(int[] array) {
array = new int[]{7, 8, 9}; // Only changes local copy!
// Caller's reference is unchanged
}
}
Proof it's pass by value:
- If Java had pass by reference,
replaceArray()would changenewArr - But
newArrstill points to{4, 5, 6} - This proves the reference was copied (pass by value)
The confusion:
- Pass by value: copy of value/reference
- Pass by reference: alias (same variable)
- Java copies the reference → pass by value
Proof with Swap Method
The Swap Method Proof
The classic proof that Java is pass-by-value:
public class SwapProof {
public static void main(String[] args) {
int a = 10;
int b = 20;
swap(a, b); // Pass by value - swap won't work!
System.out.println("a = " + a); // Still 10
System.out.println("b = " + b); // Still 20
}
static void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
// x and y are local copies
// a and b are unchanged
}
}
If Java were pass by reference:
xwould be an alias foraywould be an alias forb- Swapping
xandywould swapaandb - But it doesn't!
What actually happens:
Before swap: a=10, b=20
After call: x=10, y=20 (copies)
After swap: x=20, y=10 (local copies swapped)
Back in main: a=10, b=20 (unchanged)
To swap objects, return them:
static int[] swap(int a, int b) {
return new int[]{b, a};
}
int[] result = swap(x, y);
x = result[0];
y = result[1];
Key takeaway: Java copies the reference, not the object. This is pass-by-value.
Practice Problems
What does this code print? ```java public class StringTest { public static void main(String[] args) { String s = "hello"; modify(s); System.out.println(s); } static void modify(String str) { str = str + " world"; } } ```
Solution
Prints: `hello`
Explanation: `str = str + " world"` creates a **new** String object (since Strings are immutable) and assigns the local copy `str` to point to it. The original `s` in main() still points to `"hello"`. This is pass-by-value: the reference was copied, and changing where the copy points doesn't affect the original.What does this code print? ```java public class ObjectSwap { public static void main(String[] args) { int[] a = {1}; int[] b = {2}; swap(a, b); System.out.println(a[0] + " " + b[0]); } static void swap(int[] x, int[] y) { int[] temp = x; x = y; y = temp; } } ```
Solution
Prints: `1 2` (unchanged)
Explanation: `swap()` receives **copies** of the references `a` and `b`. Inside swap(), `x` and `y` are local copies. Reassigning `x = y` and `y = temp` only changes where the local copies point. The original `a` and `b` in main() are unaffected. This proves Java is pass-by-value.Quiz
1. Is Java pass-by-value or pass-by-reference?
2. What happens when you pass an object to a method?
3. Can a Java method modify the caller's primitive variable?
4. Why does `swap(a, b)` not work in Java?
Flashcards
Question
Is Java pass-by-value or pass-by-reference?
Click to reveal answer
Answer
Always pass-by-value. For primitives: value copied. For objects: reference copied (not the object).
Question
Why can't you swap two variables in a method?
Click to reveal answer
Answer
Method parameters are local copies. Swapping copies doesn't affect the original variables.
Question
What is the difference between == and equals()?
Click to reveal answer
Answer
== compares references (same object?), equals() compares content (logically equal?).
Question
Can a method change what an object reference points to?
Click to reveal answer
Answer
No. The reference is a copy. Changing where the copy points doesn't affect the original reference.
Question
What is Pass-by-Value in Java?
Click to reveal answer
Answer
Pass-by-Value in Java is a key concept in Java programming.
Revision Notes
Key Takeaways
- 1.Java is always pass-by-value, never pass-by-reference
- 2.For objects, the reference is copied, not the object
- 3.Methods can modify the object but not reassign the caller's reference
- 4.swap() doesn't work because parameters are local copies
Interview Tips
- •State clearly: Java is always pass-by-value
- •Explain the difference: primitives copy value, objects copy reference
- •Use swap() as the classic proof
- •Mention that == compares references, equals() compares content
Cheat Sheet
Pass-by-Value Cheat Sheet
The Rule
- Java is ALWAYS pass-by-value
- Primitives: value copied
- Objects: reference copied (not object)
What Happens
- Method gets local copy of value/reference
- Changes to primitives: lost after return
- Changes to objects: persist (via copied ref)
- Reassigning parameter: doesn't affect caller
Proof
- swap(a, b) doesn't work
- replaceArray(arr) doesn't change arr
- These prove references are copied
Common Misconception
- "Java passes objects by reference" = WRONG
- Objects appear to be by-ref because changes persist
- But the reference itself is copied (pass-by-value)