Q:

String concatenation operator

Anyone to throw right on the code and its output ? 
public class HelloWorld {
  public static void main(String[] args) {
    String s1 = "Hello World";
    String s6 = "Hello" + " World";
    String s7 = "Hello";
    String s8 = " World";
    String s9 = s7 + s8;
    String s10 = s7 + " World";
    System.out.println(s1==s6);
    System.out.println(s1==s9);
    System.out.println(s1==s10);
    System.out.println(s9==s10);
  }
}

Output : 
true
false
false
false

I understand that s1 is created in the string constant pool. I am wondering how the creation of s6, s9 and s10 due to the use of the concatenation operator.

A:

there is a lot of optimization done at compile time. So if you look at the de-compiled code after compilation you'll see like :
    String s1 = "Hello World";
    String s6 = "Hello World";
    String s7 = "Hello";
    String s8 = " World";
    String s9 = (new StringBuilder(String.valueOf(s7))).append(s8).toString();
    String s10 = (new StringBuilder(String.valueOf(s7))).append(" World").toString();
    System.out.println(s1 == s6);
    System.out.println(s1 == s9);
    System.out.println(s1 == s10);
    System.out.println(s9 == s10);

And it explains it easily. So it's compiler behind the scene, who is doing a lot of work for you.
When we concatenate the string literals, it replaces it with a single literal, when we use concatenation with variables, it uses StringBuilder.

