While interviewing candidates for a Java role at Singtel, I found that a number of developers stumbled over a simple but important question: does Java pass by reference or by value?

Discussing it afterward with some of our junior developers revealed similar uncertainty.

This short post was put together to help explain something that many developers understand intuitively, but struggle to express clearly: Java always passes arguments by value.


TL;DR

Java always passes arguments by value. This means the value of an argument is copied into the method's parameter.

  • For primitive types, the value itself is copied. Changing the parameter does not affect the caller's variable.
  • For object references, the reference value is copied, not the object. Both the caller's variable and the method's parameter initially refer to the same object. The method can modify that object's state, but assigning a different object to the parameter does not change the caller's variable.

Java does not support pass-by-reference, where a method can directly reassign the caller's variable through a parameter.

Let's unpack this.


Pass by Value

With primitive types, Java copies the value of the argument into the method's parameter.

private void doSomething(int a) {
    a = a + 1;
    System.out.println(a);
}

private void myFunction() {
    int x = 5;
    doSomething(x);
    System.out.println(x);
}

As expected, this outputs:

6
5

The value of x (5) is copied into the parameter a when doSomething is called. The value of this local parameter is incremented by 1 and printed. The result is 6.

When the method returns, we print the value of the original x variable. It is still 5.

Similarly, when passing an object to a method, Java copies the value of the reference variable into the method's parameter. Both variables then refer to the same object.

private void doSomething(Car car) {
    car.setMake("BMW");
    car.setModel("SUV X1");

    car = new Car("Ford", "Explorer");
    System.out.println(car.getMake() + " " + car.getModel());
}

private void myFunction() {
    var c = new Car("Mazda", "323");
    doSomething(c);
    System.out.println(c.getMake() + " " + c.getModel());
}

This outputs:

Ford Explorer
BMW SUV X1

In myFunction we have a variable named c which refers to a Car object in memory initialized to Mazda 323.

The variable c holds a reference to a Car object. This means:

  • it is not the object
  • its value is a reference
  • the reference it holds points to the object

When doSomething(c) is called, Java copies the value held by c into the car parameter. The car parameter now holds a copy of the reference stored in c. Both variables therefore refer to the same Car object - Mazda 323.

The doSomething method then modifies the properties of the Car object referenced by its car parameter. The car parameter points to the same object as the original c variable. Our Car object is now a BMW SUV X1.

Next, we assign a reference to a newly created Car object (Ford Explorer) to the car parameter. This change only affects the car parameter. The value of the original c variable remains unchanged.

When the method returns, c still refers to the original Car object, although that object's properties have been modified from Mazda 323 to BMW SUV X1.


Pass by Reference

In languages like C#, both ref and out allow variables to be passed by reference. With ref, the variable must already be initialised. With out, it need not be initialised, but the called method must assign it a value before returning.

public bool TryParse(string text, out int result) {    
     return int.TryParse(text, out result);
}

int x;

if (TryParse("123", out x))
{
    Console.WriteLine(x);
}

This method attempts to convert the string to an integer. On success, result receives the parsed value and the method returns true. On failure, result receives a default value and the method returns false.

Although Java doesn't support passing variables by reference, we can achieve a similar outcome by returning a value instead. For example, we can use Optional<Integer> to represent either a successfully parsed integer or the absence of a result:

public static Optional<Integer> tryParse(String text) {
    try {
        int result = Integer.parseInt(text);
        return Optional.of(result);
    } catch (NumberFormatException e) {
        return Optional.empty();
    }
}

var x = tryParse("123");

x.ifPresent(value -> System.out.println(value));

Java always passes by value.