Re: question about the toString Method



Art Cummings wrote:
You pass a reference to an object, it calls toString() on that reference.

public static void myPrintln( Object obj ) {
String s = obj.toString();
System.out.println( s ); // would actually use a different
// call here...
}

Get it?


The confusing one is how does the compiler know to call toString() when concatenating strings:

String s = "This is a: " + obj;

Answer: the compiler is specially crowbarred to call toString() in that instance.


Thank you everyone, it's a little clearer now.

Ain't the Javadocs a wonderful thing?

--
Lew
.


Quantcast