This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between Comparable and Comparator? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between Comparable and Comparator?" Watch "Difference between Comparable and Comparator?" New topic
Author

Difference between Comparable and Comparator?

mansi gupte
Ranch Hand

Joined: Dec 30, 2008
Posts: 72
Can anyone explain the difference between these two?? with an example?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41096
    
  29
Have you read the API documentation? Here, and here.

Comparable "lives inside the class" and says "I differ from other objects of the same class and I can tell you the difference."
Comparator "lives outside the class" and says "I can find out which objects differ in this respect."
You can use several Comparators on the same class.
mansi gupte
Ranch Hand

Joined: Dec 30, 2008
Posts: 72
Campbell Ritchie wrote:Have you read the API documentation? Here, and here.

Comparable "lives inside the class" and says "I differ from other objects of the same class and I can tell you the difference."
Comparator "lives outside the class" and says "I can find out which objects differ in this respect."
You can use several Comparators on the same class.


hey ,
thanks for the prompt reply.i will take this one as a reference to understand better.

PS:I read the API Doc , but got ,more confused due to the verbosity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 41096
    
  29
You're welcome
 
jQuery in Action, 2nd edition
 
subject: Difference between Comparable and Comparator?