Sub topic 7
Passing and Returning Arrays
A method is a named set of instructions. You can give an array to a method so it can read or change the array. A method can also return an array for the caller, the code that called the method, to use.
A parameter is a variable listed in a method’s declaration. An argument is the value supplied when the method is called. For example, in addBonus(scores, 5), the array scores and the number 5 are arguments.
Passing an array
An array variable holds a reference, which is a value that points to an array object. When you pass an array to a method, Java copies that reference into the parameter. Both references then point to the same array.
This means the method can change the array’s elements, and the caller will see those changes. But if the method assigns a different array to its parameter, that does not change the caller’s variable.
Worked example
Save this program as ArrayMethods.java. It changes one array, returns a new array, and then tries to replace an array through a parameter.
public class ArrayMethods {
public static void addBonus(int[] scores, int bonus) {
for (int i = 0; i < scores.length; i++) {
scores[i] = scores[i] + bonus;
}
}
public static int[] doubled(int[] scores) {
int[] result = new int[scores.length];
for (int i = 0; i < scores.length; i++) {
result[i] = scores[i] * 2;
}
return result;
}
public static void replaceLocally(int[] scores) {
scores = new int[] {99};
}
public static void main(String[] args) {
int[] scores = {10, 20};
addBonus(scores, 5);
int[] result = doubled(scores);
replaceLocally(scores);
System.out.println(scores[0]);
System.out.println(scores[1]);
System.out.println(result[0]);
System.out.println(result[1]);
}
}Compile and run it from the folder containing the file:
javac ArrayMethods.java
java ArrayMethodsThe output is:
15
25
30
50Here is what happens:
addBonusadds5to each element of the caller’s array. The array now contains15and25.doubledcreates a new array, fills it with doubled values, and returns it. The caller stores its reference inresult. The original array still contains15and25.replaceLocallyassigns a new array to its ownscoresparameter. The caller’sscoresvariable still points to the original array.
The word void means a method does not return a value. The return type int[] means a method returns an integer array. The return statement sends that array’s reference back and ends the method. In this program, static lets main call the other methods without creating an object first.
Returning arrays safely
A method does not have to create a new array before returning. It can return a reference to an existing array. In that case, the caller and any other variables holding that reference share the same array. Changes through one variable are visible through the others.
An empty array has length zero. Both loops in the example handle it: they run zero times, and doubled returns a new empty array.
A reference can also be null, meaning it points to no array. Passing null to either of those methods causes a NullPointerException, an error caused here by trying to read scores.length when there is no array. Decide whether your method should reject null or handle it before reading the array.
Exercise
If the example calls addBonus(scores, 2) instead of addBonus(scores, 5), what does doubled(scores) return?
Show answer
It returns a new array containing 24 and 44. The original array contains 12 and 22 after the bonus.
Exercise
Why does replaceLocally not change the caller’s scores variable?
Show answer
It assigns a new reference only to its own parameter. The caller’s variable still points to the original array.