Possible Duplicate:
String comparison and String interning in Java
I understand how String equals() method works but was surprised by some results I had with the String == operator.
I would have expected == to compare references as it does for other objects.
However distinct String objects (with the same content) == returns true and furthermore even for a Static String object (with the same content) which is obviously not the same memory address.
I guess == has been defined the same as equals to prevent its misuse
No,
==does just compare references. However, I suspect you’ve been fooled by compile-time constants being interned – so two literals end up refererring to the same string object. For example:From section 3.10.5 of the Java language specification: