Posted on: January 18, 2025 Posted by: rahulgite Comments: 0

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:

  • num holds the value 10.
  • The method modifyValue gets a copy of num (value 10).
  • Changing value inside the method does not affect the original num.

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:

  • person holds 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?

  1. p.name = "Alice"
    β†’ Original object is created.
  2. modify(p)
    β†’ You pass the reference value, and inside modify, you change the name of the original object.
    βœ” This change is visible outside.
  3. changeReference(p)
    β†’ Again, the reference value is passed by value.
    β†’ But assigning person = new Person(); only affects the local copy of the reference inside the method.
    ❌ It does not change the original reference p in main.

Java follows the modify behavior but not the changeReference behavior β€” because of how pass-by-value works for object references.

Leave a Comment