aspose file tools*
The moose likes Java in General and the fly likes equals and compareTo method difference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "equals and compareTo method difference?" Watch "equals and compareTo method difference?" New topic
Author

equals and compareTo method difference?

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

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"



thanks
Tanu Gulati
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14430
    
  23

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

Jesper, Matthew, Rob
thanks to All. you guys are wonderful!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals and compareTo method difference?