1. Pass by Value
- Definition: A copy of the variable’s value is passed to the method. Changes made inside the method do not affect the original variable.
- Behavior in Java: Java always uses pass by value for method arguments.
- For primitives, the value itself is copied.
- For objects, the reference (memory address) is copied, but the object itself is not passed.
Example (Pass by Value for Primitives):
public class PassByValueExample {
public static void main(String[] args) {
int num = 10;
modifyValue(num);
System.out.println("Original value: " + num); // Output: 10
}
static void modifyValue(int value) {
value = 20; // Changes local copy only
}
}
Original value: 10
Explanation:
numholds the value10.- The method
modifyValuegets a copy ofnum(value10). - Changing
valueinside the method does not affect the originalnum.
Example (Pass by Value for Objects):
class Person {
String name;
}
public class PassByValueObjects {
public static void main(String[] args) {
Person person = new Person();
person.name = "John";
modifyPerson(person);
System.out.println("Name after modification: " + person.name); // Output: Jane
}
static void modifyPerson(Person p) {
p.name = "Jane"; // Modifies the object through the copied reference
}
}
Output:
Name after modification: Jane
Explanation:
personholds a reference to an object.- The method gets a copy of the reference.
- Modifications to the object (via the copied reference) affect the original object.
2. Pass by Reference
- Definition: A reference to the variable is passed to the method. Changes made inside the method affect the original variable.
- Behavior in Java: Java does not support pass by reference. Even for objects, only the reference is passed by value, not the actual variable.
Why Java is Pass by Value?
In Java:
- Primitives: Only the value is passed.
- Objects: The reference (pointer) is passed, but it is a copy of the reference. The method cannot reassign the original reference but can modify the object’s state.
class Person {
String name;
}
public class PassByValueExample {
public static void main(String[] args) {
Person p = new Person();
p.name = "Alice";
modify(p);
System.out.println("After modify: " + p.name); // Output: Bob
changeReference(p);
System.out.println("After changeReference: " + p.name); // Output: Bob (Not Charlie!)
}
public static void modify(Person person) {
person.name = "Bob"; // Modifies the object that reference points to
}
public static void changeReference(Person person) {
person = new Person(); // A new object is created
person.name = "Charlie";
}
}
π Whatβs happening?
p.name = "Alice"
β Original object is created.modify(p)
β You pass the reference value, and insidemodify, you change thenameof the original object.
β This change is visible outside.changeReference(p)
β Again, the reference value is passed by value.
β But assigningperson = new Person();only affects the local copy of the reference inside the method.
β It does not change the original referencepinmain.
Java follows the modify behavior but not the changeReference behavior β because of how pass-by-value works for object references.