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:
"A String " + x + " and another " + y
resolves to the more efficient
new StringBuilder("A String ")
.append(x)
.append(" and another ")
.append(y).toString()
This uses the append
methods on String builder (for each type), which handle null
properly