String a = "ab"; String b = new String("ab"); System.out.println(a == b); System.out.println(a.equals(b)); System.out.println(b.intern() == a); System.out.println(a.intern() == b);
Print result:
false
true
true
false
new String will not look for the existing string in the String constant. The string.intern() method finds equal strings in the character constant pool.