Pass by Value and Pass by Reference in Java Tutorial

In this section, we will learn what the pass by value and by reference is and how they work in Java.

Passing by Reference in Java

Call by value and reference is about the way we invoke a variable to pass its content to either a parameter (via calling a method) or to another variable.

In short, we invoke a variable to pass its value in two ways:

  • By value.
  • By reference.

Call by Value: What Is Pass by Value in Java?

Let’s say we have a variable named `age` which is of type `int`. The value we can put into this variable is integer numbers.

Now, if we use this variable and assign it to another variable named `cpy`, a copy of this value will be stored into the `cpy` variable.

This means both variables have their own copy of the value and any change one does to its value, will not affect the other.

Example: passing by value in Java

public class Simple {

    public static void main(String[] args) {
        int age = 100;
        int cpy = age;
        age = 200;
        System.out.println(cpy);
    }
}
Output: 100

As you can see, even though we’ve changed the value of the `age` variable, the value of the `cpy` is still 100 and that proves each of these two variables has its own independent value.

This is called pass by value, and it happens when we pass the value of variables to another variable.

Basically, when passing primitive values (`byte`, `short`, `int`, `double`, `float`, `char`, `boolean`, `long`) from one variable to another, the entire content is copied to the target variable.

Another example:

public class Simple {

    public static void main(String[] args) {
        int age = 100;
        change(age);
        System.out.println(age);
    }
    public static void change(int age){
        age = 200;
    }
}
Output: 100

In this example, we sent the value of the `age` variable to the `change()` method and in the body of this method; the value is changed to 200. But still when the execution backs to the main method to run the `println` method, we see that the value 100 is still stored in the `age` variable.

So when we called the `change()` method, a copy of the value stored in the `age` variable is passed to this method and so now we have two completely independent values.

How to Pass by Reference in Java?

The `var` in this example only stores the address of the created object, but not the object itself! (The object is stored in the heap memory and this variable only has an address or basically a link to that object)

We know and seen that via this variable (the address that is stored in the variable) we can access the object and work with its member.

Now let’s say we passed this value to another variable like this:

ClassName var2 = var;

Here we’ve passed a copy of the memory-address of the object that `var` is pointing at, to another variable named `var2`.

So currently both variables have a memory address that points to the same object in the heap.

This means if the `var` variable changes the content of one-member variable of the target object, the `var2` also sees this change and that’s only because this variable also has the memory address of the same object. 

This is called pass by reference. Basically, we’re still passing the value of a variable to another one (copy) but this value is a memory address and not the target object itself!

Java Call by reference notes:

  • When we pass the value of a variable that is pointing to an object, to another variable, we’re passing the memory address of that object, not the object itself. So the copy happens on the address value.
  • The values of both variables (`var` and `var2`) are independent from each other (basically these two variables can point to any other object that they want) but for now they have the same address and that’s the reason of why one variable can change the content of the target object and the other can see the change as well.

Note: this is also true for arrays as well. Basically arrays are also stored in the heap memory and the variable that holds this array is actually holding the memory address of that array. So we can pass this address to other variables and they’ll have the same access to the target array as well. This means a variable can change the content of the array and this will be reflected on other variables as well (Only because they all have the same memory address).

Example: passing by reference in Java

public class Person {
    String name;
    String lastName;

    public Person(String name, String lastName) {
        this.name = name;
        this.lastName = lastName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

public class Simple {

    public static void main(String[] args) {
        Person person1 = new Person("John","Bauer");
        Person person2 = person1;
        person2.setLastName("Doe");
        System.out.println(person1.getLastName());
    }
}
Output: Doe

In this example, we’ve created an object from the `Person` class and stored its memory address to the `person1` variable.

Next, we stored this memory address to another variable named `person2`.

Now both the `person1` and `person2` are pointing to the same object (because they have the same memory address) and so any change each of these two variables does to the object, the other variable will see that as well.

For example, here we used the `person2` variable to change the value of the `lastName` attribute of the object (we changed the lastName from `Bauer` to `Doe`).

The `person1` variable also saw this change when we called the `person1.getLastName()` method.

Again, all this is because both variables have the same object memory-address.

This is true when we pass the memory address of an object as the argument to a method as well. Basically, all we do is passing a copy of the memory address of an object to another variable (in this case the parameter of a method) and in that case both variables will have the same memory address and any change one does, the other will see too.

Example:

public class Simple {

    public static void main(String[] args) {
        Person person1 = new Person("John","Bauer");
        change(person1);
        System.out.println(person1.getLastName());
    }
    public static void change(Person prs){
        prs.setLastName("Doe");
    }
}
Output: Doe

Another example:

public class Simple {

    public static void main(String[] args) {
        int list[] = {1,2,3,4,5,6,7};
        change(list);
        System.out.println(list[0]);
    }
    public static void change(int[] l){
        l[0] = 1000;
    }
}
Output: 1000

In this example, the `list` variable stored the memory address of the array that is created here.

Next, a copy of this address is sent to the `change()` method and so the variable `l` in that method has the same memory address as the `list` variable.

In the body of the `change()` method, we changed the value of the first element in the array to 1000 and so the `list` variable will also see this change as well (again because it has the memory address of this array).

 Difference between Call by Value and Call by Reference

In short, call-by-value means copy the current value in the target variable and send that copy! This means the receiver variable (or parameter) will have an independent value and so any change either variables make, won’t affect the other.

On the other hand, pass by reference means pass only the memory address of the target value (object)! This means both the original variable and the receiver variable both are pointing to the same object and hence, if one of them changes the state of an object, the other will see this change. 

In Java, pass by reference is still a kind of passing by value! Because a variable is still passing a copy of a value! But this time the value is the memory address of an object that is located in the memory. So the object itself is not copied, but its memory address does!

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies