3 ms·
Trying it in the console just now I see javascript does it too >"hey "+null "hey null" I wonder why the language designers thought that was a good ide
by tim333 9y ago
Trying it in the console just now I see javascript does it too
>"hey "+null
"hey null"
I wonder why the language designers thought that was a good idea?
- kpil 9y agoThe reason is that the string concatenation operation is doing a String.valueOf() on the argument, and that delegates to toString() that promises a human readable string that is relevant. For some reason someone decided to check for null instead of the stricter option of throwing a NullPointerException. Maybe to help debugging or to follow the gist of toString(). I guess it was too late to change even in Java 0.9... Autoboxing, iterators and other newer features are more strict,so I suppose it's just one of the few irregularities left from prehistoric times...