new String(”xyz“)
for sure creates a new instance. "abc"
and "xyz"
are stored in the class constant pool, x = x + y
creates a StringBuilder
under the hood and therefore creates a new String
, so the count of strings are 4 here.
The compiler might substitute x + y
with a constant ("xyzabc"
), though.