Complete the Primitive types vs references exercises with calls before reviewing the solutions.

Review the primitive exercise solution with AP CS Tutor Brandon Horn.

Original code

public static void main(String[] args)
{
    Coordinate2D e = new Coordinate2D(1,1);
    reference2(e);
    System.out.println(e);
}

private static void reference2(Coordinate2D f)
{
    f = new Coordinate2D(2,2);
}

Output

(1, 1)

Explanation

All arguments in Java are passed by value, including references. The call reference2(e); passes a copy of the value of e to the reference2 method. The value of e is the memory address of the object containing x: 1, y: 1.

The reference2 method changes the actual value of f, which has no effect on the value of e.

Step by step with memory diagrams

Step 1

public static void main(String[] args)
{
    Coordinate2D e = new Coordinate2D(1,1);
    // more code not yet run
}

Memory diagram after Step 1

diagram showing e pointing to a box containing x and y, each with value 1

Except for the variable name, this is the same as Reference exercise 1 with call Step 1.

Step 2

public static void main(String[] args)
{
    Coordinate2D e = new Coordinate2D(1,1);
    reference2(e);
    // more code not yet run
}

private static void reference2(Coordinate2D f)
{
    // more code not yet run
}

Step 2 is immediately after the call to the reference2 method, but before any code inside the reference2 method has been run.

Memory diagram after Step 2

diagram showing e and f pointing to the same box containing x and y, each with value 1

Except for the variable names, this is the same as Reference exercise 1 with call Step 2.

Step 3

public static void main(String[] args)
{
    Coordinate2D e = new Coordinate2D(1,1);
    reference2(e);
    // more code not yet run
}

private static void reference2(Coordinate2D f)
{
    f = new Coordinate2D(2,2);
}

Step 3 is immediately after execution of the statement f = new Coordinate2D(2,2);, but before the reference2 method returns.

Memory diagram after Step 3

diagram showing e pointing to a box containing x with value 1 and y with value 1, and f pointing to a different box containing x with value 2 and y with value 2

The statement f = new Coordinate2D(2,2); creates a new object and points f at it (sets the value of f to the memory address of the object). Changing the actual value of f does not change the value of e. All arguments in Java are passed by value.

Contrast this with the reference1 method in Reference exercise 1 with call. The reference1 method runs a mutator method on the object to which both c and d point.

Step 4

public static void main(String[] args)
{
    Coordinate2D e = new Coordinate2D(1,1);
    reference2(e);
    System.out.println(e);
}

private static void reference2(Coordinate2D f)
{
    f = new Coordinate2D(2,2);
}

Step 4 is after the reference2 method returns and after the print statement executes.

Memory diagram after Step 4

diagram showing e pointing to a box containing x and y, each with value 1

When the reference2 method ends, f no longer exists. In Java, when the last reference to an object is removed, the object is considered garbage and can no longer be accessed.

Output after Step 4

(1, 1)

The main method prints e, which runs the toString method on the object to which e refers.

Additional classes & objects resources

Comments

Comment on Primitive types vs references with method calls