String interning riddle

Viewed 181

I am struggling with the following riddle of my coworker:

public class App1 {
  public static void main(String[] args) {
    String s1 = "Ja".concat("va"); // seems to be interned?!
    String s2 = s1.intern();

    System.out.println(s1 == s2); // true
  }
}

This outputs true. I am little bit surprised because it looks like s1 is interned. But this is no constant expression, isn't it?

But then I am even more surprised why the following prints false.

public class App2 {
  public static void main(String[] args) {
    String s1 = "Ja".concat("va"); // seems not to be interned?!
    String s3 = new String("Java"); // this changes output
    String s2 = s1.intern();

    System.out.println(s1 == s2); // false
  }
}

Why does the introduction of s3 change the output?

6 Answers
Related