Possible Duplicate:
Concatenating null strings in Java
I’m just wondering if someone can explain why does the following code works the way it does:
String a = null;
String b = null;
String c = a + b;
System.out.println(c.toString());
This prints “nullnull” to the console.
I was kind of expecting that the operation + would thrown an exception or, to a lesser degree, that “c” would be null.
“String conversion,” required by the concatenation operator, mandates that null values are to be mapped to the four characters
nullwhenever a string representation is needed.Spec for string conversion: http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.11
Spec for string concatenation: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.1