Possible Duplicate:
Difference Between Equals and ==
String comparison and String interning in Java
Pretty self-explanatory question this time.
EDIT: I understand == is not equals. What I did not understand is why both a in “a” == “a” were assigned or treated as if they were same object instance (they are).
EDIT DID SOMEBODY READ THE QUESTION OR JUST LIKE PRESSING CLOSE BUTTON? IT HAS NOTHING TO DO WITH ANY OF THESE ABOVE. TELL ME WHERE DO I MENTION EQUALS METHOD…..
"a" == "a"gives true because “a” will be treated as String literal and pooled. So, both “a” points to same instance, because both references points to same object, == returns true.When you say
new String("a"). Brand new object will be created on Heap and different references, so == returns false, you need to use.equals().