Java course

Method arguments, references, and overloads

1.6.2 — Method arguments, references, and overloads

Java passes a copy of each argument’s value to a method. For an int, that value is a number. For an array, it is a reference to an array object.

A method such as static void change(int value) accepts an integer and gives back no result. int[] means an array of integers; its first index is 0. Each cell supplies its own method and data.

Changing an int parameter

The parameter value receives a copy of the number in count. Assigning a new number to that parameter does not change the caller’s variable.

Read the two output lines: the value inside the method changes, but the caller’s count stays at 5.

Two references can identify one array

Assigning an array variable to another variable copies the reference, not the elements. Both variables then identify the same array.

The next cell changes the first element through alias. Reading it through originalValues shows that same change.

A method can change the shared array

An array parameter receives a copy of the reference. The method can use that reference to modify an element in the shared array. This is called mutation.

values[0] = 9; changes an array element; it does not assign a new array to the parameter.

Replacing the parameter is different

values = new int[] {9, 9}; makes the parameter refer to a new array. It does not replace the caller’s reference.

Compare this output with the previous example. The caller still reads its original array.

Make a copy before changing elements

For an int array, clone() creates a separate array with the same numbers. Changing that copy leaves the input array alone.

The next method loops through the copy and adds 1 to each element. i < result.length visits only valid indexes. The method returns the new array.

Arrays.toString(...) formats all array values for printing; its import is included below.

Same name, different parameter types

Overloading means declaring methods with the same name but different parameter lists. Java chooses a matching method using the argument types.

The next example has one method for int and another for double. 3 is an int literal; 3.5 is a double literal. The printed labels show which method was selected.

Changing only a return type is not enough to create an overload.

Practice: keep the input unchanged

The method below currently changes the input array. Replace int[] result = values; with int[] result = values.clone();. Leave the rest unchanged.

Run it. It should print Input: [5] and Result: [6].

Hint

Use values.clone() on the right side of =. That creates an array separate from the input.

Worked solution
import java.util.Arrays;
static int[] practiceCopy(int[] values) {
    int[] result = values.clone();
    for (int i = 0; i < result.length; i = i + 1) {
        result[i] = result[i] + 1;
    }
    return result;
}
int[] practiceInput = {5};
int[] practiceResult = practiceCopy(practiceInput);
System.out.println("Input: " + Arrays.toString(practiceInput));
System.out.println("Result: " + Arrays.toString(practiceResult));

Try other arrays

In your completed cell, change only the values assigned to practiceInput:

practiceInputExpected InputExpected Result
{}[][]
{0}[0][1]
{5, 7}[5, 7][6, 8]

The input should stay unchanged in every case. An empty array is safe because the loop runs zero times.

Enable JavaScript to run the Java notebook.