I would like to understand how memory is allocated when we use + and += on Strings in Java. I know that String literals are stored in the String Constant Pool and in Case #1, both s1 and s2 reference the same memory in String Constant Pool.
In Case #2, eventhough I use a + operator, it still references the same object in String Constant Pool
What I find interesting is Case #3. How and where is memory allocated in this case? How is it different from Case #2
//Case #1
String s1 = "Hello Java";
String s2 = "Hello Java";
System.out.println(s1 == s2); //true
//Case #2
s1 = "Hello" + " Java";
s2 = "Hello Java";
System.out.println(s1 == s2); //true
s1 = "Hello";
s1 += " Java";
s2 = "Hello Java";
System.out.println(s1 == s2); //false
This isn't actually a difference between
+and+=; you'd get the same behavior as Case #3 if you wrote:The reason for the behavior you see in Case #2 is that
"Hello" + " Java"is a constant expression [JLS8 §15.28], so it can actually be handled at compile-time exactly as if it were"Hello Java", and is required to be interned just as"Hello Java"is.When you break it up into separate statements, that's no longer the case, so you get a newly created string [JLS8 §15.18.1].