public class stringEquals{
public static void main(
String[] args){
String x1="abc";
String x2="ab";
x2=x2+"c";
if(x1==x2)
System.out.println("both do refer to same object");
if(x1.equals(x2))
System.out.println("both objects have same content ");
}
}
what is the answer to this problem i guessed answer would be
both do refer to same object
both objects have same content
since firstly a x1 reference is refering to object containing "abc"..this object goes to String object pool.
then another object is created that has content as ab and is refered by x2
this object also goes to String object pool
now when the third line excutes another object containing "c" is added to object pool and lost immediately ..then another object is created with contents "abc" and placed in object pool but now JVM sees it has a object containing same literal previously so it does not create a new object instead causes x2 refer to same object as x1...
so x1 has same bit
pattern as x2 then why x1==x2 prints out false?
please answer me ..
Regards