Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

all about comapring objects

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java in General (Beginner.)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic