• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

all about comapring objects

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone tell me in detail about the following and what is the difference between

equals, == and compareTo methods?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java in General (Beginner.)
 
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic