What is the output displayed by the following program?
public class
test {
public static void main(
String a[])
{
String s1 = "ab";
String s2 = "abcd";
String s5 = "abcd";
String s3 = "cd";
String s4 = s1 + s3;
s1 = s4;
System.out.print("s1 "+((s1 == s2)? "==" : "!=")+" s2\n");
System.out.print("s5 "+((s5 == s2)? "==" : "!=")+" s2");
}
}
I thought it should be s1 == s2 and s5 == s2. because all the 4 strings s1, s2,s4 and s5 point to the same literal "abcd". But I find that answer is s1 != s2. Can some one explain?