wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes all about comapring objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "all about comapring objects" Watch "all about comapring objects" New topic
Author

all about comapring objects

Arun Prasath
Ranch Hand

Joined: Jul 18, 2006
Posts: 60
Can anyone tell me in detail about the following and what is the difference between

equals, == and compareTo methods?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
With objects, the == operator compares the two object references and tells whether they refer to the same object. If you've done C it's a lot like comparing two pointers.

equals() is implemented in code to compare the value or content of two objects. It often compares a combination of fields. For example, a Person class might say

compareTo() compares the values again but tells whether one object is "greater than" or "less than" the other. That Person class might order by last name:

Both of these methods need a bit more housekeeping. They should check for nulls, only compare to other instances of exactly the same class and so on.

An important rule is to make equals(), compareTo() and hashcode() compatible. If two objects are equal according to one, they should be equal in all three.

Did that help?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Moving to Java in General (Beginner.)


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40061
    
  28
[NITPICK]
equals(), compareTo() and hashcode() compatible. If two objects are equal according to one, they should be equal in all three.
Not quite. You can, at least in theory, have two different objects return identical hash codes.[/NITPICK]
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by Campbell Ritchie:
[NITPICK] Not quite. You can, at least in theory, have two different objects return identical hash codes.[/NITPICK]


Indeed. Furthermore, the API for compareTo() specifies that compareTo() does not have to return equal simply because equals() does (and vice-versa).


It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."


- Adam
[ July 31, 2006: Message edited by: Adam Nace ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Good catch, guys. I once had written if equals() says true the others should agree. That much I think is actually true. Donno why I changed it. And I did say "should" rather than "MUST" on the other.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: all about comapring objects