The compiler optimizes storage of String literals by reusing them.
publicclass MainClass {
publicstaticvoid main(String[] argv) {
String s1 = "YES";
String s2 = "YES";
// The String literal "YES" appears in both lines 1 and 2,
// but the compiler creates only one String object, referred to by both s1
// and s2.
if (s1 == s2){
System.out.println("equal");
}
String s3 = new String("YES");
String s4 = new String("YES");
if (s3 == s4){
System.out.println("s3 eq s4");
}
}
}