Strange String pool behavior

"555" + ""

is a compile-time constant, whereas

giveLiteralString() + ""

isn’t. Therefore the former compiles into just the string constant “555” and the latter compiles into the actual method invocation and concatenation, resulting in a fresh String instance.


Also see JLS ยง3.10.5 (String Literals):

Strings computed by concatenation at run time are newly created and
therefore distinct.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)