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

coors points to a box representing the array. The array points to 3 boxes representing Coordinate2D objects. The objects contain x: 1, y: 1, x: 2, y: 2, and x:3, y: 3

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

coors and coorsInside point to the same box representing the array. The values in the array are the same as in step 1

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

coors points to the same array as in Step 3. The array to which coors points points to the same objects as in Step 2. coorsInside points to a different array containing the values null, null, and null.

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

coors points to the same array as in Step 3. The array to which coors points points to the same objects as in Step 2. coorsInside points to a different array. The array to which coorsInside points points to an object containing the values x: 5, y: 5 and 2 null values.

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

coors points to a box representing the array. The array points to 3 boxes representing Coordinate2D objects. The objects contain x: 1, y: 1, x: 2, y: 2, and x:3, y: 3

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)]

Comments

Comment on Arrays as objects exercises