Is Java "pass-by-reference" or "pass-by-value"?

Is Java “pass-by-reference” or “pass-by-value”?

Hey Shreshtha seth,

Pass-by-Value in Java :

Java is strictly pass-by-value. When you pass a variable to a method, a copy of the value of the variable is passed, not the variable itself. For primitive data types like int, float, etc., the value itself is passed. Changes made to the parameter inside the method do not affect the original variable. For objects, a copy of the reference to the object is passed. This means both the original reference and the copy point to the same object in memory. Changes to the object’s state inside the method are reflected outside the method because they both refer to the same object. This behavior can sometimes be confusing, as it looks like pass-by-reference for objects, but it’s actually pass-by-value with the value being a reference to the object.

Pass-by-Reference :

Pass-by-reference means passing a reference (or memory address) of the variable to the method. Any changes made to the parameter inside the method directly affect the original variable. In Java, variables are never passed by reference. They are always passed by value, either the value itself (for primitives) or a copy of the reference (for objects). Some programming languages, like C++, support pass-by-reference explicitly using reference types (e.g., & in C++), where changes to the parameter inside the method affect the original variable.

Hey Shresthaseth,

Consider the following Java code: Pass-by-Value in Java:

public static void main(String[] args) { int x = 10; changeValue(x); System.out.println(x); // Output: 10 }

public static void changeValue(int a) { a = 20; // Does not change the original value of x }

In this example, x is passed to the changeValue method. However, a is a separate variable that receives a copy of the value of x (which is 10). Changing a inside the method does not affect the original value of x.

Hey Shreshthaseth,

Pass-by-Value in Java:

In Java, when you pass a primitive type to a method, you’re passing a copy of the value. Changes to the parameter inside the method do not affect the original value. Here’s a simple example:

public class PassByValueExample { public static void main(String[] args) { int x = 10; System.out.println("Before: " + x); // Output: Before: 10 modifyValue(x); System.out.println("After: " + x); // Output: After: 10 }

public static void modifyValue(int a) {
    a = 20;  // Modifying the parameter 'a'
}

}

In this example, x is passed by value to modifyValue() method. Even though a is modified inside the method, it does not affect the original value of x because a is a separate copy of x.

Pass-by-Reference in Java (Not Supported):

Java does not support pass-by-reference. When you pass an object to a method, you’re passing a copy of the reference, not the actual object. Changes to the object’s state inside the method are reflected outside because both the original reference and the copy point to the same object. However, the reference itself is passed by value. Here’s an example:

public class PassByReferenceExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder(“Hello”); System.out.println("Before: " + sb); // Output: Before: Hello modifyReference(sb); System.out.println("After: " + sb); // Output: After: Hello World }

public static void modifyReference(StringBuilder str) {
    str.append(" World");  // Modifying the object referred to by 'str'
}

}

In this example, sb is passed to modifyReference() method. Although str is a copy of the reference to sb, changes made to the object’s state (appending " World") inside the method are visible outside because both sb and str point to the same StringBuilder object.