This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

equals and compareTo method difference?

 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me, equals and compareTo both do the same basic job(comparing the objects) why does java have two different methods for doing the same work ( i know i am missing some thing here but want to know what).

some of my questions
1.) can one class have both equals and compareTo (if yes what is the use)
2.) which one is always better to use in a class, "equals" or "compareTo"


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals() checks if two objects are the same or not and returns a boolean.

compareTo() (from interface Comparable) returns an integer. It checks which of the two objects is "less than", "equal to" or "greater than" the other. Not all objects can be logically ordered, so a compareTo() method doesn't always make sense.

Note that equals() doesn't define the ordering between objects, which compareTo() does.

1. Yes.
2. Neither is better, it depends on what functionality your class needs.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of additions:

- There is always an equals() method, because it's defined in Object. You only override it if you want different behaviour from the default (which is "are these refencing the same object?"). You don't always have a compareTo() method.

- If you do have a compareTo method, it is strongly recommended that you also provide an equals method that is consistent with it. That is, if a.compareTo(b) == 0, then a.equals(b) (and vice-versa).
 
Sheriff
Posts: 22773
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals is much broader than compareTo. I can say that an apple is not equal to a pear. I can't go comparing them though.

This is reflected in the argument type: Object for equals, T for Comparable<T>.
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper, Matthew, Rob
thanks to All. you guys are wonderful!!
 
Marshal
Posts: 78693
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote: . . . it is strongly recommended that you also provide an equals method that is consistent with it. That is, if a.compareTo(b) == 0, then a.equals(b) (and vice-versa).

Recommended, not compulsory.

Example:
A lot of people rant about the BigDecimal#compareTo(java.math.BigDecimal) not being "consistent with equals", but I believe that is the correct way to implement it.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic