The above println() can be executed without a problem.
Does this mean that the object referenced by a
is copied to the monitor?
No — a temporary String
object is created by the object's toString()
method,
and the characters in that object are sent to the monitor by println().
Here is the example program with yet another change:
import java.awt.*; class PointEg3 { public static void main ( String arg[] ) { Point a = new Point(); // declarations and construction combined Point b = new Point( 12, 45 ); Point c = new Point( b ); System.out.println( a ); // create a temporary String based on "a", print it out System.out.println( b ); // create a temporary String based on "b", print it out System.out.println( c ); // create a temporary String based on "c", print it out } }
The program prints out:
java.awt.Point[x=0,y=0] java.awt.Point[x=12,y=45] java.awt.Point[x=12,y=45]
This program is deceptively short; its execution calls for quite a bit of activity.
Just as the program is about to close, how many objects have been created and how many object references are there? Has any garbage been created?