jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparable interface" Watch "Comparable interface" New topic

Comparable interface

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Can any body explain me Comparable interface with an example or can give a link for the example ...

thanks a lot .
Srinivas Velamuri

Joined: Apr 02, 2001
Posts: 28
I am attaching an example, which shows how to use Comparable interface. In this example, I am comparing Rabbits based on their ear length. Let me know if you have any questions over this example:

* @author Srinivas Velamuri
public class ComparableExample {

public static void main(String[] args) {
Rabbit myRabbit = new Rabbit(7); // my Rabbit with 7 inches ears
Rabbit[] rabbits = new Rabbit[10];
for (int i=0; i < rabbits.length; i++) {
rabbits[i] = new Rabbit(i+2);
if ( myRabbit.compareTo(rabbits[i]) > 0) {
System.out.println ("myRabbit is bigger than the rabbit at position " + i + " in the Rabbit collection");
} else if ( myRabbit.compareTo(rabbits[i]) < 0) {
System.out.println ("myRabbit is smaller than the rabbit at position " + i + " in the Rabbit collection");
} else {
System.out.println ("myRabbit is same than the rabbit at position " + i + " in the Rabbit collection");




class Rabbit implements Comparable {
private int earLength; // in inches

Rabbit(int earLength){ // constructor
this.earLength = earLength;

public int compareTo(Object anotherRabbit) {
if (this.earLength > ((Rabbit)anotherRabbit).getEarLength()) {
return 1;
} else if (this.earLength < ((Rabbit)anotherRabbit).getEarLength()) {
return -1;
} else {
return 0;

public int getEarLength() {
return earLength;


Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
As you know Java sorts objects on their natural order when inserting them into a sorted collection.
When no natural order is readily apparent (which means in just about every case except for built-in things like String or primitive wrappers) you can implement Comparable to provide the VM with instructions on what that natural order is.

For this purpose Comparable defines the int compareTo(Object o) method.
This method is to be defined to return a positive integer value in case the object being compared TO is smaller than the object being compared on, 0 in case they are identical for sorting order, and negative if the compared on object is the smaller one.

After that everything is pretty much automatic.

What I sometimes do is implement compareTo and then use the result of that in equals, returning false whenever the result of compareTo is not 0 (and of course returning false in case comparison isn't possible, which in case of compareTo should throw a ClassCastException).

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks Srinivas & Jeroen ,

As per your explanation I also made a simple example :

thanks once again .
I agree. Here's the link: http://aspose.com/file-tools
subject: Comparable interface
It's not a secret anymore!