Understanding “Pass by Value” and “Pass by Reference” in Java

be11amer

Bellamer

Posted on November 22, 2024

Understanding “Pass by Value” and “Pass by Reference” in Java

When it comes to method arguments, one of the most misunderstood topics in Java programming is the difference between “pass by value” and “pass by reference.” In this blog post, we’ll explore these concepts in depth, including how Java actually handles method arguments, and provide illustrative code examples to clarify what’s happening behind the scenes.

What Do “Pass by Value” and “Pass by Reference” Mean?

Pass by Value:

In “pass by value,” a method receives a copy of the actual value of the argument passed to it. Any modifications made to the parameter within the method do not affect the original argument.

Pass by Reference:

In “pass by reference,” a method receives a reference (or address) to the actual memory location of the argument. Modifications to the parameter directly affect the original object since the method operates on the same memory.

How Does Java Handle Method Arguments?

In Java, all arguments are passed by value. However, this statement can be misleading because what gets passed by value differs depending on whether the argument is a primitive type or an object reference.

  1. Primitive types: The value itself is copied and passed to the method.
  2. Objects (non-primitives): The reference to the object is copied and passed to the method. While the reference itself is a copy, it still points to the same object in memory.

This distinction can create confusion, leading some developers to mistakenly believe Java supports “pass by reference.” Let’s break this down with code examples.

Pass by Value: Primitive Types

When you pass a primitive type to a method, Java creates a copy of the actual value. Changes to the parameter inside the method do not affect the original variable.

public class PassByValueDemo {
    public static void main(String[] args) {
        int num = 10;
        System.out.println("Before method call: " + num); // Output: 10
        modifyPrimitive(num);
        System.out.println("After method call: " + num); // Output: 10
    }

    public static void modifyPrimitive(int n) {
        n = 20; // Changes the copy, not the original
        System.out.println("Inside method: " + n); // Output: 20
    }
}

Enter fullscreen mode Exit fullscreen mode

Explanation:

• The value of num is copied and passed to the modifyPrimitive method.
• Inside the method, changes are made to the local copy (n), but num in the main method remains unchanged.

Pass by Value: Object References

For objects, the reference (memory address) is passed by value. This means the method operates on the same object the reference points to, but the reference itself is a copy. Let’s illustrate this:

Example 1: Modifying Object State

class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}

public class PassByValueObject {
    public static void main(String[] args) {
        Person person = new Person("Alice");
        System.out.println("Before method call: " + person.name); // Output: Alice
        modifyObject(person);
        System.out.println("After method call: " + person.name); // Output: Bob
    }

    public static void modifyObject(Person p) {
        p.name = "Bob"; // Modifies the same object
        System.out.println("Inside method: " + p.name); // Output: Bob
    }
}

Enter fullscreen mode Exit fullscreen mode

Explanation:

• The reference to the person object is copied and passed to the modifyObject method.
• Both the original and the copied reference point to the same object in memory, so changes to the object’s state are visible outside the method.

Example 2: Reassigning Object References

Now let’s see what happens if you try to reassign the reference itself within the method.

public class PassByValueReassign {
    public static void main(String[] args) {
        Person person = new Person("Alice");
        System.out.println("Before method call: " + person.name); // Output: Alice
        reassignObject(person);
        System.out.println("After method call: " + person.name); // Output: Alice
    }

    public static void reassignObject(Person p) {
        p = new Person("Charlie"); // Reassigns the local copy of the reference
        System.out.println("Inside method: " + p.name); // Output: Charlie
    }
}

Enter fullscreen mode Exit fullscreen mode

Explanation:

• The reference person is copied and passed to the reassignObject method.
• Inside the method, the local copy (p) is reassigned to point to a new object (new Person("Charlie")), but this does not affect the original reference in the main method.
• The original person reference in main still points to the "Alice" object.

Under the Hood: What Happens in Memory?

Primitive Types:

• The value is stored directly in the memory location associated with the variable.
• When passed to a method, the value is copied to a new memory location for the parameter.

Object References:

• The reference (memory address) of the object is stored in the variable.
• When passed to a method, the reference is copied, so both the original and the copied references point to the same object in the heap memory.

Visualizing the Behavior

Example: Object Reference

Person person = new Person("Alice"); // Memory layout
// Stack (local variables):
// person -> address: 0x1234

// Heap (objects):
// 0x1234 -> Person{name="Alice"}

Enter fullscreen mode Exit fullscreen mode

When passed to a method:
• A new reference (e.g., p) is created, which also points to 0x1234.

Key Takeaways

  1. Java is always “pass by value.” • For primitives: The value itself is copied. • For objects: The reference is copied, but both references point to the same object.
  2. Modifying the state of an object affects the original object because the method operates on the same object.
  3. Reassigning the reference within a method does not affect the original reference outside the method.

By understanding these nuances, you’ll be better equipped to reason about Java method calls and avoid common pitfalls in your programs.

💖 💪 🙅 🚩
be11amer
Bellamer

Posted on November 22, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related