Does Java pass by reference or pass by value?

First of all we should understand what is meant by pass by value or pass by reference.

passed by reference

When a parameter is passed by reference , the caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller's variable.

passed by value

When a parameter is passed by value , the caller and callee have two independent variables with the same value. If the callee modifies the parameter variable, the effect is not visible to the caller. When passing by ref you are basically passing a pointer to the variable. Pass by value you are passing a copy of the variable. In basic usage this normally means pass by ref changes to the variable will see be the calling method and pass by value they won’t. The Java Spec says that everything in Java is pass-by-value . There is no such thing as "pass-by-reference" in Java. Unfortunately, they decided to call the location of an object a "reference". When we pass the value of an object, we are passing the reference to it. This is confusing to beginners . Example
import java.time.LocalDate; import java.time.Period; class Student { private String name; public Student(){} public Student(String nm){; } public String getname() { return name; } public void setname(String name) { = name; } }
public class TestClass { public static void main(String[] args) { Student john = new Student("John"); Student dow = new Student("Dow"); swap(john, dow); System.out.println("john name is "+john.getname()); System.out.println("dow name is "+dow.getname()); } public static void swap(Object o1, Object o2){ Object temp = o1; o1=o2; o2=temp; } }
john name is John dow name is Dow