LeetCampus
Interview Question

Java works as “pass by value” or “pass by reference” phenomenon?

July 24, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

Pass by Value vs. Pass by Reference is a fundamental concept in programming, particularly in languages like Java. Interviewers often ask this question to assess a candidate's understanding of how Java handles method arguments and the implications of this behavior on memory management and performance. The core distinction is that Java uses pass by value, meaning that a copy of the variable is passed to methods. However, this can be nuanced, as objects are also passed by value, but the reference to the object is what gets copied, not the object itself. This can lead to confusion about whether Java is truly pass by reference. Understanding this concept is crucial for effective programming in Java, as it influences how variables behave when modified within methods. Common pitfalls include assuming that changes to an object inside a method affect the original object outside the method, which can lead to unexpected bugs if not understood properly. Moreover, recognizing this behavior is essential for optimizing code and managing memory efficiently in real-world applications.

Sample Answers

Example 1: Understanding Pass by Value

In Java, when you pass a primitive type (like int, float, etc.) to a method, you are passing a copy of that variable. This means that any changes made to that variable within the method do not affect the original variable outside the method. For instance, consider the following code:

public class PassByValueExample {
    public static void main(String[] args) {
        int number = 5;
        modifyValue(number);
        System.out.println(number); // Output will be 5
    }

    public static void modifyValue(int num) {
        num = 10; // This change does not affect 'number'
    }
}

Here, the modifyValue method attempts to change the value of num, but since num is a copy of number, the original number remains unchanged. This illustrates the core concept of pass by value for primitives.

Example 2: Objects and References

When dealing with objects in Java, the situation is slightly different. Even though Java is still pass by value, what gets passed is the reference to the object, not the object itself. This means that changes to the object's attributes within the method do affect the original object. For example:

class MyClass {
    int value;
}

public class PassByReferenceExample {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.value = 5;
        modifyObject(obj);
        System.out.println(obj.value); // Output will be 10
    }

    public static void modifyObject(MyClass myObj) {
        myObj.value = 10; // This change affects the original object
    }
}

In this case, modifyObject modifies the value attribute of the original MyClass object. This shows how references behave in Java, leading to a common misconception that Java is pass by reference.

Example 3: Common Misconceptions

A frequent misconception is that Java works as pass by reference due to how object references behave. However, it’s essential to clarify that while the reference is passed by value, the original object can still be modified. Here’s an example that demonstrates this:

public class MisconceptionExample {
    public static void main(String[] args) {
        int[] array = {1, 2, 3};
        modifyArray(array);
        System.out.println(array[0]); // Output will be 99
    }

    public static void modifyArray(int[] arr) {
        arr[0] = 99; // Modifying the content of the array
    }
}

In this example, while the reference to the array is passed by value, the method modifies the contents of the array. This highlights the importance of understanding how references work in Java and can help prevent bugs in larger applications.

Keywords

Javapass by valuepass by referencememory managementmethod arguments

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions