Hi All,
I am preparing for SCJP1.4 from Kathy Sierra and Bert Bates book: Jave 2 Sun Certified PRogrammer and Developer. On Pg 359-360, Important Facts About Strings and Memory, the author explains about how is String s = "abc" different from String s = new String("abc"); From that explanation.
If I do:
String s1 = "abc";
String s2 = "abc";
if(s1 == s2)
System.out.println("Strings are equal"); //this will be executed.
But if I change the above code to:
String s1 = "abc";
String s2 = "ab";
s2 += "c";
if(s1 == s2)
System.out.println("Strings are equal"); //this will not be executed.
In this case s1 == s2 turns out to be false. Why? Why didn't the compiler referred s2 to the same String object "abc", which was already in String constant pool, as s1 was referring to it?
Can any one please explain me this?
Many Thanks,
Avi