Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

== vs equals

7 views
Skip to first unread message

Ken K

unread,
Sep 13, 2004, 3:31:35 PM9/13/04
to
In some open source I've looked at recently the author always used the
equals method rather than the equals operator. For objects defined
from user-defined classes is there some reason why the method is
preferred?

Ken

Sudarshan Raghavan

unread,
Sep 13, 2004, 4:44:38 PM9/13/04
to
Ken,

== checks for the equality of object references, while .equals() checks
if two objects instances are equal. The catch is, not all classes override
the .equals() method and invoking this method on an instance calls the
default method provided by the Object class. The default method is
analogous to using the == operator. Thus programmers usually override
this method in their classes so that it can be used to check for instance
equality rather than reference equality.

A classic example is the String class where string1 == string2 does not
necessarily produce the same result as string1.equals(string2). Check if the
user defined classes in the code that you are looking at have overridden
the .equals() method.

-- Bali

"Ken K" <k...@kenkast.com> wrote in message
news:a6f4b7b2.04091...@posting.google.com...

0 new messages