Demo
class
public class Demo
{
public static void main(String[] args)
{
Coordinate2D c1 = new Coordinate2D(5, 6);
System.out.println(c1);
/* paused here */
}
}
When c1
is printed, Java checks if c1
points to (stores the memory address of) an object. c1
does point to an object. Java automatically runs the toString
method on the object to which c1
points. The Coordinate2D toString
method returns a String
in the form (x, y)
.
The code segment prints:
(5, 6)
Memory diagram
The diagram is the same as in Step 3.