When used with object refernces, the == operator checks to see if the references refer to same object. In your case o and s2 refer to the same object because they were made aliases when you did:
Object o = s2;
This made o refer to the object which s2 was refering to at the time, and therefor the equality test will return true.
Remember though, that the operands of the == operator (when comparing references) must be found in the same class heirarchy.
When you use the new key word you are creating a new String object here for s1. Then for s2 you are referring to a second object. s3 simply latches onto the same object s2 is referring to because of string pooling. Therefore s1==s2 will be false because they are two different objects. s1.equals(s2) or s1.equals(s3) will return true because now you are comparing the contents of the objects.
Fes
naraharirao mocherla
Ranch Hand
Joined: Aug 16, 2005
Posts: 45
posted
0
HI Kavin Kumar according to ur code.
whenver u create a string with new key word.it will be given a memory address. if u create a string using just String s1= "abc"; then this string will be in a pool. if u create another string with same contents ,but without new keyword then that string will go the already existing string pool.(where "abc "is there);
So , s1==s3 gives "NOT GOOD":// references are not same. s3==s4 gives "EQ".references are same.
s1.equals(s3) gives "Good".// character by character is exactly the same.
According to ur code
nagaraj raja
Ranch Hand
Joined: Aug 06, 2005
Posts: 36
posted
0
Hai == USED TO COMPARE REFERENCE VARIABLES REFEREING TO SAME TO CLASS OR NOT .equals() USED TO COMPARE THE CONTENT OF THE OBJECTS