4 ms·
>being cast to 'null' curing concatenation What does this mean?
by cced 10y ago
>being cast to 'null' curing concatenation
What does this mean?
- tantalor 10y ago"" + null is "null"
- ams6110 10y agoYou're kidding, surely? Java really does that? "" + null should be null (or throw an exception). Null is null and nothing else.
- deleted 10y ago[deleted]
- fgonzag 10y agoI think it should be an exception as those types can not be concatenated. But still , "" + null being "null" is just plain crazy. I could see "" + null == "" (as you are concatenating nothing to the string), or "" + null == null as you are concatenating a string to null, but "null" is bonkers.
- tantalor 10y agoThis is why I shrug when people complain about JavaScript casting weirdnesses
- LgWoodenBadger 10y agoThe + operator in this case is compiled to call String.valueOf() which returns "null" when given null.
- tantalor 10y agoNot kidding. http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#valueOf%28java.lang.Object%29 http://docs.oracle.com/javase/6/docs/api/java/lang/String.ht... http://stackoverflow.com/questions/4260723/concatenating-null-strings-in-java http://stackoverflow.com/questions/4260723/concatenating-nul...
- LgWoodenBadger 10y agoNobody writes "" + null. They write "" + myVariableRef. And myVariableRef is null. As I posted earlier, the code is compiled to "" + String.valueOf(myVariableRef). Since the empty String is a valid String, and Strinfg.valueOf() also returns a String, what other behavior would you ever imagine being sane or possible?
- abritinthebay 10y agoAn empty string of it HAS to be a string would make infinitely more sense. However a type error wouldn't toady on a strongly typed language
- avz 10y ago> Since the empty String is a valid String, and Strinfg.valueOf() also returns a String, what other behavior would you ever imagine being sane or possible? IMHO, String.valueOf() throwing NPE upon encountering a null reference would be both sane and possible. Unfortunately, it does indeed appear that it returns "null" instead [1]. [1] https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#valueOf(java.lang.Object) https://docs.oracle.com/javase/7/docs/api/java/lang/String.h...
- samuellb 10y agoOn the other hand, it's quite convenient to not have to do null checks in debug messages. Compare: log.trace("doStuff(" + obj1 + ", " + obj2 + ")"); to this: log.trace("doStuff(" + (obj1 != null ? obj1 : "null") + ", " + (obj2 != null ? obj2 : "null") + ")"); The second one is IMHO quite hard to read, even in this short example (and code readability is important when you do code reviews)
- tomtomtom777 10y agoThat is why you should use string formatting instead of concatenation. For string formatting, rendering "null" (or "") makes sense; for coercion not.
- landryraccoon 10y agoAren't you glad Java doesn't allow operator overloading, so this sort of mischief is impossible? /s
- deleted 10y ago[deleted]
- carlio 10y agoShould have been "during" but I cannot edit my comment so I suppose that typo will live in prosperity.
- woliveirajr 10y agoPosterity?
- carlio 10y agoThat's the joke