3 ms·
String.valueOf(null) returns "null" though, and that is what "foo" + null uses. I can understand why it would be surprising if you thought it used .toString, bu
by Asooka 9y ago
String.valueOf(null) returns "null" though, and that is what "foo" + null uses. I can understand why it would be surprising if you thought it used .toString, but it's clearly listed in the standard.
- LukeShu 9y agoSure, but .toString() can be used to change how your class stringifies, so clearly it is being called at some point (perhaps by String.valueOf()). And indeed, public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java#l2969 http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/s...