If a String is formed by concatenating two String literals it will also be interned.
String a = "x";
String b = a + "y"; // a is not a string literal, so no interning
------------------------------------------------------------------------------------------
String b = "x" + "y"; // on the other hand, "x" is a string literal
String c = "xy";
System.out.println( b == c ); // true
Here is a commonly found example on string interning in Java
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other { static String hello = "Hello"; }
followed by it's output
true
true
true
true
false
true