Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
Look at the call addBonus(scores, 3) and the method declaration addBonus(int[] values, int bonus). The call supplies two arguments: scores and 3. The declaration names two parameters: values and bonus. When the method call runs, the first argument supplies a value to the first parameter, and the second argument supplies a value to the second parameter.
int[] scores = {12, 7, 19};
addBonus(scores, 3);
static void addBonus(int[] values, int bonus) {
values[0] += bonus;
values = new int[]{0};
}The names do not have to match. scores is the name used by the caller, while values is the name used inside addBonus. The call connects them by their positions, not by spelling. Inside the method, values and bonus are the names you use for the supplied values.
In addBonus(scores, 3) and addBonus(int[] values, int bonus), which are the arguments and which are the parameters?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The first argument always targets the first parameter, and the second argument targets the second parameter. In addBonus(scores, 3), scores matches int[] values because both occupy position one, and 3 matches int bonus because both occupy position two. The declared parameter types still have to accept the supplied values.
addBonus(scores, 3); // correct order
addBonus(3, scores); // incompatible types at both positions
addBonus(scores); // one argument is missing
addBonus(scores, 3, 4); // one argument is extraChanging the order does not make Java search for a better match. With addBonus(3, scores), the integer 3 is offered to int[] values and the array scores is offered to int bonus, so the call does not compile. Omitting or adding an argument also prevents compilation because the number of supplied values must match the number of parameters.
When addBonus(scores, 3) runs, Java evaluates the two arguments and copies their values into the parameters. For scores, that value is the array reference stored in the variable. For 3, that value is the integer 3. Java does not copy the three array elements for this call, and it does not pass the scores variable itself.
After the method starts, values contains a copy of the reference that scores contained. Both variables can therefore lead to the same array, but they remain separate variables. bonus contains its own copied integer value, 3. If the method changes values, that changes the parameter variable, not the caller's scores variable.
What two values are copied into the parameters when addBonus(scores, 3) is called?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The first statement inside the method uses the copied reference to reach the original array. Since values and scores both point to that array, values[0] += bonus changes its first cell from 12 to 15. The array now contains [15, 7, 19], and the caller sees that changed element through scores.
static void addBonus(int[] values, int bonus) {
values[0] += bonus;
values = new int[]{0};
}
int[] scores = {12, 7, 19};
addBonus(scores, 3);
System.out.println(Arrays.toString(scores)); // [15, 7, 19]The second statement changes a different thing: values = new int[]{0} gives the parameter a reference to a newly created one-cell array. It does not change the reference stored in scores. After the assignment, values points to [0], while scores still points to the original array [15, 7, 19]. The caller therefore prints [15, 7, 19].