I have the following code
System.out.println("" + null);
and the output is null
.
How does Java do the trick in string concatenation?
javastring
I have the following code
System.out.println("" + null);
and the output is null
.
How does Java do the trick in string concatenation?
Best Answer
Because Java converts the expression
"A String" + x
to something along the lines of"A String" + String.valueOf(x)
In actual fact I think it probably uses
StringBuilder
s, so that:resolves to the more efficient
This uses the
append
methods on String builder (for each type), which handlenull
properly