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