The expression "abc" + "d" is a constant expression, so concatenation is performed at compile time, which leads to the code equivalent:
String s1 = "abc"; String s2 = "abcd"; String s3 = "abcd"; String s4 = s1 + "d";
The expression s1 + "d" not a constant expression and therefore is executed at run time, creating a new string object. Therefore, although s2 and s3 refer to the same string object (due to the constant internment of strings), s2 and s4 refer to different (but equal) string objects.
For more information on constant expressions, see JLS Section 15.28 .
Jon skeet
source share