Back
Why does the following work? I would expect a NullPointerException to be thrown.
String s = null;s = s + "hello";System.out.println(s); // prints "nullhello"
String s = null;
s = s + "hello";
System.out.println(s); // prints "nullhello"
The second line is transformed to the following code:
s = (new StringBuilder()).append((String)null).append("hello").toString();
The append methods can handle null arguments.
31k questions
32.8k answers
501 comments
693 users