Does Java pass by reference or pass by value?

Understanding the concepts of "pass by value" and "pass by reference" is essential in programming.

Passed by reference

When a parameter is passed by reference, both the caller and the callee share the same variable for the parameter. If the callee modifies the parameter variable, the changes become visible to the caller's variable as well. This behavior allows for direct manipulation of the original data passed as the parameter, impacting both the caller's and callee's perspective on the shared variable.

Passed by value

When a parameter is passed by value, the caller and callee have two separate and independent variables that hold the same value. Any modifications made to the parameter variable within the callee's scope do not affect the original variable in the caller's scope. This behavior ensures that changes made to the parameter remain localized to the callee's context, and the original value in the caller's scope remains unchanged. Java primarily uses pass by value, which means that when primitive data types or object references are passed as arguments to methods or functions, a copy of their value or reference is made, ensuring data encapsulation and preventing unintended side effects.

When passing by reference, a pointer to the variable's memory address is passed, allowing changes made to the parameter within the called function to directly affect the original variable in the caller's scope. Thus, modifications to the parameter are visible to the calling method.

The Java language strictly adheres to pass-by-value semantics, and there is no pass-by-reference mechanism. However, the confusion often arises because Java uses the term "reference" to represent the location of an object in memory. When we pass an object as an argument to a method, we are actually passing the value of the reference, not the object itself. As a result, changes made to the object within the method do affect the original object since both the caller and the callee are still pointing to the same object in memory.

This terminology can be confusing for beginners who might expect pass-by-reference behavior similar to some other programming languages. Understanding the distinction between pass-by-value and the passing of object references is crucial for proper understanding and effective coding in Java.

Example
import java.time.LocalDate; import java.time.Period; class Student { private String name; public Student(){} public Student(String nm){ this.name=nm; } public String getname() { return name; } public void setname(String name) { this.name = name; } }
public class Main { public static void main(String[] args) { Student student1 = new Student("Smith"); Student student2 = new Student("Warner"); swap(student1, student2); System.out.println("Student-1 name is "+student1.getname()); System.out.println("Student-2 name is "+student2.getname()); } public static void swap(Object o1, Object o2){ Object temp = o1; o1=o2; o2=temp; } }
Output
Student-1 name is Smith Student-2 name is Warner

Conclusion

Pass by reference allows for direct access to the original variable, enabling changes to be seen by the calling method, while pass by value involves working with a copy of the variable, ensuring that the original variable remains unchanged in the calling method.