Is toString() called when a reference of String class is printed in JAVA? -
this question has answer here:
when printing reference of other type string, output name of class along hashcode / string returned tostring() method. when reference of string printed, actual "string values" shown, tostring() called in case ? 
no. if @ the source code println(string), you'll see:
public void println(string x) { synchronized (this) { print(x); newline(); } } the difference between , generic object-signature version string.valueof not called:
public void println(object x) { string s = string.valueof(x); synchronized (this) { print(s); newline(); } } and string.valueof method contains call tostring:
public static string valueof(object obj) { return (obj == null) ? "null" : obj.tostring(); } you should used browsing source files of jdk if you're curious details such this.
Comments
Post a Comment