The below program outputs false
String s1="a";
String s2="b";
String s3=s1+s2;
String s4="ab";
if(s3==s4)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
and this code outputs true
String s3="a"+"b";
String s4="ab";
if(s3==s4)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
Shouldn't the output in first case be true? As while creating String s4="ab" there is already an object with value "ab" in the string constant pool.