Is java pass by reference? The answer is NO. Java is always pass-by-value. Today we will learn java pass by reference and java pass by value with examples.
Let’s create Person class:
package com.example;
public class Person{
String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Create main class:
package com.example;
public class Example {
public static void main(String[] args){
Person person;
person = new Person("Jone Doe");
System.out.println(person.getName());
updateName(person);
System.out.println(person.getName());
}
public static void updateName(Person reference){
reference.setName("Max");
}
}
When you pass an Object into a method you’re passing the Object reference and not the Object itself. See the updateName method. We are passing Person reference and set the name using setName setter method.
Now think of what an Object’s reference/variable does:
- A variable holds the bits that tell the JVM how to get to the referenced Object in memory (Heap).
- When passing arguments to a method you ARE NOT passing the reference variable, but a copy of the bits in the reference variable. Something like this: 3bad086a. 3bad086a represents a way to get to the passed object.
- So you’re just passing 3bad086a that it’s the value of the reference.
- You’re passing the value of the reference and not the reference itself (and not the object).
- This value is actually COPIED and given to the method.
So what happens in the Example class?
- The variable person is created but it’s null at the beginning.
- A new Person Object is created and stored in memory, and the variable person is given the reference to the Person object. That is, its address. Let’s say 3bad086a.
- The variable person holding the address.
- Called the updateName method with person reference/address.
- The variable/reference person is copied bit-by-bit and passed to updateName inside the function.
- No new instances of Person are created.
- Both “person” and “updateName” hold the same value of 3bad086a.
- Both variables have IDENTICAL COPIES of the reference and they both refer to the same Person Object, the SAME Object on the Heap and NOT A COPY.
Check weather it is same reference or not?
Updated Version of Example class
package com.example;
public class Example {
public static void main(String[] args){
Person person = new Person("Jone Doe");
System.out.println(person.getName());
updateName(person);
System.out.println(person.getName());
System.out.println(person); // this prints the memory address
}
public static void updateName(Person reference){ // don't create any person obj
System.out.println(reference); // this prints the memory address
reference.setName("Max");
}
}
Output:
Jone Doe
com.example.Person@30f39991
Max
com.example.Person@30f39991
We can see the person reference are same. SO,
You always pass a copy of the bits of the value of the reference!
- If it’s a primitive data type these bits will contain the value of the primitive data type itself.
- If it’s an Object the bits will contain the value of the address that tells the JVM how to get to the Object.
SO, finally says that Java is always pass by value.