Does Java support pass-by-reference ?

In this article, we will answer the question: Does Java support pass-by-reference? The answer is no; Java does not support pass-by-reference. Instead, Java uses pass-by-value for all variable passing. However, how Java handles object references can sometimes create confusion, making it seem like pass-by-reference. Let’s dive into examples to clarify these concepts.

Pass-by-Value with Primitives

When you pass a primitive type (like int, char, etc.) to a method, the value is copied, and the method works with the copy. Any changes to the parameter inside the method do not affect the original variable.

public class PassByValueExample {
    public static void main(String[] args) {
        int number = 5;
        modifyPrimitive(number);
        System.out.println("After method call, number is: " + number); // Output: 5
    }

    static void modifyPrimitive(int num) {
        num = 10;
    }
}

In this example, the original variable number remains unchanged because the method works with a copy of the value.

Pass-by-Value with Objects

When you pass an object to a method, the value of the reference (the memory address) is copied, not the object itself. The method receives a copy of the reference, which still points to the same object. Thus, changes made to the object inside the method will affect the original object.

public class PassByValueExample {
    public static void main(String[] args) {
        MyObject obj = new MyObject();
        obj.value = 5;
        modifyObject(obj);
        System.out.println("After method call, obj.value is: " + obj.value); // Output: 10
    }

    static void modifyObject(MyObject obj) {
        obj.value = 10;
    }
}

class MyObject {
    int value;
}

In this example, the modifyObject method changes the value property of the original object because both the original reference and the copied reference point to the same object.

Reassigning Object References

If you reassign the object reference inside the method, the original reference outside the method will not be affected.

public class PassByValueExample {
    public static void main(String[] args) {
        MyObject obj = new MyObject();
        obj.value = 5;
        reassignObject(obj);
        System.out.println("After method call, obj.value is: " + obj.value); // Output: 5
    }

    static void reassignObject(MyObject obj) {
        obj = new MyObject();
        obj.value = 10;
    }
}

class MyObject {
    int value;
}

In this example, reassignObject creates a new MyObject and assigns it to the local parameter obj. This does not affect the original obj reference in the main method.

Conclusion

To summarize, Java passes parameters by value. For primitive types, this means a copy of the value is passed. For objects, this means a copy of the reference is passed, allowing methods to modify the object’s fields but not the reference itself.

Leave a Reply

Your email address will not be published. Required fields are marked *

Join the Tribe