This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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: 37945
    
  22
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: 37945
    
  22
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Difference between Comparable and Comparator?
 
Similar Threads
difference RAD & WSAD
Difference between wsad5.1.2 & Rad
Synchonized
JATO Vs JDOM
Run as AWT or SWT Application Eclipse Question