Complete the Arrays as objects exercises before reviewing the solutions.
Review the Arrays as objects exercise 4 solution with AP CS Tutor Brandon Horn.
Original code
public static void main(String[] args)
{
Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(1, 1);
coors[1] = new Coordinate2D(2, 2);
coors[2] = new Coordinate2D(3, 3);
System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]
mystery4(coors);
System.out.println(Arrays.toString(coors));
}
public static void mystery4(Coordinate2D[] coorsInside)
{
coorsInside = new Coordinate2D[3];
coorsInside[0] = new Coordinate2D(5, 5);
}
Output
[(1, 1), (2, 2), (3, 3)]
[(1, 1), (2, 2), (3, 3)]
Explanation
The mechanics are identical to those in Exercise 2.
The statement coorsInside = new Coordinate2D[3];
changes the actual value of coorsInside
, which has no effect on the value of coors
.
Step by step memory diagram
Step 1
public static void main(String[] args)
{
Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(1, 1);
coors[1] = new Coordinate2D(2, 2);
coors[2] = new Coordinate2D(3, 3);
System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]
// additional code not yet run
}
Memory diagram after Step 1
This is the same as in the Exercise 3 solution.
Output after Step 1
[(1, 1), (2, 2), (3, 3)]
Step 2
public static void main(String[] args)
{
Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(1, 1);
coors[1] = new Coordinate2D(2, 2);
coors[2] = new Coordinate2D(3, 3);
System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]
mystery4(coors);
// additional code not yet run
}
public static void mystery4(Coordinate2D[] coorsInside)
{
// additional code not yet run
}
Memory diagram after Step 2
This is also the same as in the Exercise 3 solution.
Output after Step 2
[(1, 1), (2, 2), (3, 3)]
Step 3
public static void main(String[] args)
{
Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(1, 1);
coors[1] = new Coordinate2D(2, 2);
coors[2] = new Coordinate2D(3, 3);
System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]
mystery4(coors);
// additional code not yet run
}
public static void mystery4(Coordinate2D[] coorsInside)
{
coorsInside = new Coordinate2D[3];
// additional code not yet run
}
Memory diagram after Step 3
As in Exercise 2, the statement ... = new Coordinate2D[3];
creates a new array (in this case containing the values [null, null, null]
). The statement coorsInside = ...
changes the actual value of coorsInside
. Changing the value of coorsInside
does not effect the value of coors
. coors
still points to the original array, which remains unchanged.
Output after Step 3
[(1, 1), (2, 2), (3, 3)]
Step 4
public static void main(String[] args)
{
Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(1, 1);
coors[1] = new Coordinate2D(2, 2);
coors[2] = new Coordinate2D(3, 3);
System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]
mystery4(coors);
// additional code not yet run
}
public static void mystery4(Coordinate2D[] coorsInside)
{
coorsInside = new Coordinate2D[3];
coorsInside[0] = new Coordinate2D(5, 5);
}
Memory diagram after Step 4
As in Exercise 2, the statement coorsInside[0] = ...
changes the value at index 0
in the array to which coorsInside
points. This has no effect on coors
, the array to which coors
points, or any of the objects inside the array to which coors
points.
Output after Step 4
[(1, 1), (2, 2), (3, 3)]
Step 5
public static void main(String[] args)
{
Coordinate2D[] coors = new Coordinate2D[3];
coors[0] = new Coordinate2D(1, 1);
coors[1] = new Coordinate2D(2, 2);
coors[2] = new Coordinate2D(3, 3);
System.out.println(Arrays.toString(coors));
// prints: [(1, 1), (2, 2), (3, 3)]
mystery4(coors);
System.out.println(Arrays.toString(coors));
}
Memory diagram after Step 5
When mystery4
returns (end), coorsInside
(the parameter) goes away. Since nothing points ot the array containing [(5, 5), null, null]
, it goes away too. coors
points to the original array, that still contains the original values.
Output after Step 5
[(1, 1), (2, 2), (3, 3)]
[(1, 1), (2, 2), (3, 3)]