HOWTO · Java

Array Passed By Value or Passed By Reference in Java

Learn whether Java passes arrays by value or by reference, and why it passes array references by value.

On this page

Java passes every method argument by value. When the argument is an array, the value copied into the parameter is a reference to the array object. The caller and the parameter can therefore refer to the same object, but they remain separate variables. This explains why changing an element is visible to the caller while assigning another array to the parameter is not.

How Java Passes Arguments

A reference is a value that lets code access an object; it is not the object itself. A method receives a copy of that reference value. At the start of a call, the caller’s array variable and the method parameter refer to the same array object. Java does not pass an array variable itself by reference, so the method cannot replace the caller’s variable by assigning to its own parameter.

The distinction is between changing an object and changing a variable. An assignment to an array element changes the shared object. An assignment to the parameter changes only the parameter’s copied reference. These two operations have different effects even though both happen inside the called method.

How Array Element Changes Affect the Caller

When a method writes to values[index], it changes an element in the array object both references can reach. The caller sees that changed element after the method returns. The reference was passed by value; the element mutation is visible because the copied reference still leads to the same object.

What Happens When a Method Reassigns an Array Parameter

Assigning a new array to the parameter makes that parameter refer to a different object. It does not change the caller’s variable, which still refers to the original array. The caller can see mutations made to the original object before reassignment, but it does not receive the parameter’s new reference. To replace the caller’s array variable, return the new array and assign the return value at the call site, or mutate the original array instead.

How Primitive Parameters Behave

Primitive arguments also pass by value: the parameter starts with a copy of the primitive value. If a method assigns a different number to that parameter, the caller’s variable remains unchanged. This is like reassigning an array parameter, not like changing an array element. The array case adds a reference: its copied value can be used to mutate the shared object, while assigning a new reference affects only the local parameter.

Run this self-contained example:

public class ArrayArgumentDemo {
    static void changeFirst(int[] values) {
        values[0] = 9;
    }

    static void replaceParameter(int[] values) {
        values = new int[] {7, 8};
    }

    static void changeNumber(int number) {
        number = 9;
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2};
        changeFirst(numbers);
        System.out.println(numbers[0]);
        replaceParameter(numbers);
        System.out.println(numbers[0]);

        int count = 1;
        changeNumber(count);
        System.out.println(count);
    }
}
9
9
1

The first print shows the changed element. The second shows that the caller’s array is still unchanged after the method parameter is assigned a different array. The last print shows that the caller’s primitive variable also remains unchanged.

Key Takeaway

Java always passes arguments by value, including array references. Element mutation is visible when the caller and method parameter refer to the same array object. Reassigning the parameter changes only its local reference, and assigning to a primitive parameter changes only its local copy. Keeping object mutation separate from parameter assignment makes the behavior predictable.