News for hackers. Stuff that matters.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by cced on Sunday September 04, 2016 @08:39PM (#12427462)
>being cast to 'null' curing concatenation

What does this mean?

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by tantalor on Sunday September 04, 2016 @08:42PM (#12427470)
"" + null is "null"
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by landryraccoon on Sunday September 04, 2016 @11:44PM (#12427968)
Aren't you glad Java doesn't allow operator overloading, so this sort of mischief is impossible? /s
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by ams6110 on Sunday September 04, 2016 @09:44PM (#12427621)
You're kidding, surely? Java really does that?

"" + null should be null (or throw an exception). Null is null and nothing else.

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by LgWoodenBadger on Sunday September 04, 2016 @10:44PM (#12427801)
Nobody 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?

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by avz on Monday September 05, 2016 @02:59AM (#12428604)
> 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.h...

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by samuellb on Monday September 05, 2016 @04:19AM (#12428876)
On 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)

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by tomtomtom777 on Monday September 05, 2016 @04:53AM (#12428991)
That is why you should use string formatting instead of concatenation.

For string formatting, rendering "null" (or "") makes sense; for coercion not.

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by richardwhiuk on Monday September 05, 2016 @06:16AM (#12429271)
That just resolves into everyone always doing formatting, and never doing concatenation, which yields the same problem.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by ajuc on Monday September 05, 2016 @03:35AM (#12428718)
It should just throw NPE. I understand why people writing quick debug printlns could be infuriated by that, but the way it works now leads to much more subtle bugs.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by abritinthebay on Sunday September 04, 2016 @11:02PM (#12427843)
An 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
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by fgonzag on Sunday September 04, 2016 @10:25PM (#12427751)
I 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.

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by tantalor on Sunday September 04, 2016 @10:30PM (#12427773)
This is why I shrug when people complain about JavaScript casting weirdnesses
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by LgWoodenBadger on Sunday September 04, 2016 @10:25PM (#12427752)
The + operator in this case is compiled to call String.valueOf() which returns "null" when given null.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by tantalor on Sunday September 04, 2016 @10:29PM (#12427768)
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by carlio on Monday September 05, 2016 @02:06AM (#12428432)
Should have been "during" but I cannot edit my comment so I suppose that typo will live in prosperity.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by woliveirajr on Monday September 05, 2016 @07:28AM (#12429537)
Posterity?
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by carlio on Monday September 05, 2016 @08:00AM (#12429667)
That's the joke