Does Java pass by value or by reference?

In this article I will answer the long asked question by beginners, “is Java pass by value or reference?“.

First, we need to understand the variables

Before we come to the is Java pass by value or reference question, first we have to understand Java variables.

In Java, variables can hold two things. For primitive types, the variables hold their values. For objects, the variables hold their references.

Consider this example code:

import java.util.ArrayList;

public class Prototyping {
   public static void main(String[] args) {
      ArrayList<String> cars = new ArrayList<>();
      ArrayList<String> autos = cars;

      autos.add("BMW");

      System.out.println("cars.size(): "+cars.size());
      System.out.println("autos.size(): "+autos.size());
   }
}

Is Java passed by value or reference?When you execute the code above, you will find that size() method executed on cars and autos variables return both 1, despite that we executed add() method only on autos variable.

This situation has occurred because we haven’t copied the cars object to autos. We have copied the object reference from cars variable to autos variable. Both of those variables now contained reference to the same object in memory, which is ArrayList created in the first statement of the main method.

 

Passing variables to methods

Now that we have observed that variables hold references to object, we can learn about another important aspect of Java before we will answer the “is Java pass by value or reference?” question.

When we pass an object to a method, we pass in fact a copy of object reference. Not the object value, not its reference, but a copy of reference.

Consider following example code:

import java.util.ArrayList;

public class Prototyping {
   public static void main(String[] args) {
      ArrayList<String> cars = new ArrayList<>();
      cars.add("BMW");
      cars.add("Volkswagen");
      cars.add("Mercedes-Benz");

      System.out.println("cars size before calling the method: "+cars.size());
      replaceWithEmptyList(cars);
      System.out.println("cars size after calling the method: "+cars.size());

   }

   public static void replaceWithEmptyList(ArrayList<String> list) {
      list = new ArrayList<String>();
   }
}

When we execute the code above, we will get the following result:

cars size before calling the method: 3
cars size after calling the method: 3

Why replaceWithEmptyList(cars) haven’t replace the object held in cars variable with a new, empty one? Because the list variable in the replaceWithEmptyList method was a copy of the cars reference. By assigning a new object to the list variable, we have simply replaced the reference being held in the list variable. As the previous reference was just a copy of cars reference, nothing got changed in our original object.

Does Java pass by reference or value?

Is Java pass by value or reference?

Now that we have learned the concepts explained above, we can answer the question is Java pass by value or reference?

Java pass value of object reference. Therefore, we can say that Java is “pass by value”. But to understand this answer, we have to understand the concepts from above.

Learn more about Java