Java Arrays as Method Parameters: Two Ways to Pass Arrays, Do You Know?

In Java programming, arrays are a very commonly used data structure, and the way arrays are passed as method parameters is a basic knowledge point that beginners need to master. Sometimes, we may find that after modifying array elements in a method, the original array changes; sometimes, when we reassign an array in a method, the original array does not change. This is actually due to two different parameter passing methods at work. Today, we will discuss the two passing methods of Java arrays as method parameters in detail.

1. Basic Review: Java Parameter Passing is “Pass by Value”, and Arrays are Objects

First, it is crucial to clarify a core concept: Parameter passing in Java is always “pass by value”. This means that when we pass a variable to a method, the method receives a copy of the variable’s “value”.

For primitive data types (such as int, double, etc.), the value itself is passed (e.g., passing 5 to a method results in the parameter inside the method being 5). However, arrays in Java are objects, so when an array is passed as a parameter, the reference to the array is passed (which can be understood as the “address” of the array in memory). This is similar to giving someone the key to your house; they can use the key to operate on the items in the room, but if they replace the key with a new one, the original key (original array reference) can still open the original door (original array).

2. First Method: Modifying Array Elements (Original Array is Changed)

When a method receives an array parameter and operates on the elements of the original array through the array reference, the content of the original array will be modified.

Example:
Suppose we have an array [1, 2, 3], and we need to write a method to increment each element of the array by 1. This method will directly modify the elements of the original array.

import java.util.Arrays;

public class ArrayParamDemo {
    // Method: Increment each element of the array by 1
    public static void addOne(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i]++; // Modify array elements (operate on the original array)
        }
    }

    public static void main(String[] args) {
        int[] original = {1, 2, 3};
        System.out.println("Before method call: " + Arrays.toString(original)); // Output: [1, 2, 3]

        addOne(original); // Pass the original array to the method
        System.out.println("After method call: " + Arrays.toString(original)); // Output: [2, 3, 4]
    }
}

Why is the original array modified?
Because the parameter arr in the addOne method and original point to the same array object (they have the same reference). When the method executes arr[i]++, it actually modifies the elements of the original array original at the corresponding positions.

3. Second Method: Modifying the Array Reference (Original Array is Not Changed)

If the method modifies the “reference” of the array parameter (i.e., makes the parameter point to a new array), the original array’s reference will not be changed, and the content of the original array will not be affected.

Example:
Suppose we write a method that makes the parameter array point to a newly created array. At this time, the reference of the original array remains the same, so the content of the original array remains unchanged.

import java.util.Arrays;

public class ArrayParamDemo2 {
    // Method: Make the parameter array point to a new array
    public static void changeArray(int[] arr) {
        // Assign a new array to arr (now arr points to the new array)
        arr = new int[]{5, 6, 7, 8};
    }

    public static void main(String[] args) {
        int[] original = {1, 2, 3, 4};
        System.out.println("Before method call: " + Arrays.toString(original)); // Output: [1, 2, 3, 4]

        changeArray(original); // Pass the original array to the method
        System.out.println("After method call: " + Arrays.toString(original)); // Output: [1, 2, 3, 4] (unchanged!)
    }
}

Why is the original array unchanged?
The arr in the method is a “copy of the reference” of original. When we execute arr = new int[]{5,6,7,8}, we only make this “reference copy” point to a new array. The reference of original is not modified, so the original array original still points to the original array.

4. Core Differences Between the Two Methods

Passing Method Operation Within the Method Is the Original Array Modified?
Method 1 (Modify Elements) Modify elements of the original array via the reference Yes
Method 2 (Modify Reference) Modify the parameter’s reference to point to a new array No

5. Common Misconceptions and Precautions

A common mistake for beginners is: thinking that when an array is passed as a parameter, the entire array is copied, so modifications within the method will not affect the original array. However, in reality, the array is passed by “reference”. As long as the method operates on the elements of the original array through this reference, it will affect the original array. Only when the method modifies the parameter’s reference (to point to a new array) will the original array remain unaffected.

6. Summary

When a Java array is passed as a method parameter, a “reference” (memory address) of the array is passed. There are two core methods:
1. Modify Elements: The method operates on the elements of the original array through the reference, and the content of the original array will change;
2. Modify Reference: The method makes the parameter point to a new array, the reference of the original array remains unchanged, and the content is not affected.

Mastering these two methods allows you to flexibly handle parameter passing and modification logic of arrays in methods and avoid common errors!

Xiaoye