What type of parameter does the System.out.println()
method expect?
It expects a reference to a String as a parameter.
toString()
Actually, System.out.println()
can deal with several types of parameters,
and one of them is reference to a String.
No version of System.out.println()
was written to use a Point
reference, however.
So the following does not look like it will work:
Point a = new Point(); // a is a Point reference
System.out.println( a );
|
+--- should be String reference
However, it does work, for the following reason:
When a parameter should be aString
reference, but a reference to another type of object is given, Java will call the object'stoString()
method to get aString
reference.
All objects (of any type at all) have a toString()
method, although
for some objects it is not very useful.
The above println()
can be executed without a problem.
Does this mean that the object referenced by a
is copied to the monitor?