why use Comparable interface?

J

jesssi1203

Hi,
I am new to Java and I was wondering what's the difference between
implementing the Comparable interface, and just adding a "int
compareTo" method in the class? Essentially they're doing the same
thing, right? Thanks.
 
R

Robert Klemme

Hi,
I am new to Java and I was wondering what's the difference between
implementing the Comparable interface, and just adding a "int
compareTo" method in the class? Essentially they're doing the same
thing, right? Thanks.

You cannot use the second variant with TreeSet unless you provide a custom
Comparator implementation. The interface ensures type compatibility,
i.e., you can use your type wherever a Comparable is allowed.

Kind regards

robert
 
J

john

I am new to Java and I was wondering what's the difference
between implementing the Comparable interface, and just
adding a "int compareTo" method in the class? Essentially
they're doing the same thing, right? Thanks.

interface 'Comparable' :

Lists (and arrays) which contain objects which implement
the interface 'Comparable' can be sorted automatically by
Collections.sort (and Arrays.sort).

interface 'Comparator' :

A class which implements the interface 'Comparator' can be
passed as an argument to sort-methods, like Collections.sort.
In such a class you can then define, in method 'compare',
how sorting should take place.
 
C

Chris Uppal

I am new to Java and I was wondering what's the difference between
implementing the Comparable interface, and just adding a "int
compareTo" method in the class? Essentially they're doing the same
thing, right?

Implementing the Comparable interface is telling the Java type-checker (and the
runtime system too) that you /have/ provided a compareTo() method. Without
that information you will not be allowed to use your objects where a Comparable
is expected even though they do have the "right" method.

-- chris
 
E

Eric K Idema

Hi,
I am new to Java and I was wondering what's the difference between
implementing the Comparable interface, and just adding a "int
compareTo" method in the class? Essentially they're doing the same
thing, right? Thanks.

If you don't declare your class as implementing Comparable, there
is no way for other code to know that you've implemented compareTo.
Any code that uses Comparable will either accept it as a method
parameter or cast to it:

void sort( Comparable[] c ) {

}

or

void sort( Object[] o ) {
Comparable[] c = (Comparable[]) o;
}

If you don't declare your class to implement Comparable, then you
won't be able to use either of those methods. In the first case
the compiler will stop you and in the second a ClassCastException
will be thrown at runtime.

Eric
 
R

Roedy Green

I am new to Java and I was wondering what's the difference between
implementing the Comparable interface, and just adding a "int
compareTo" method in the class? Essentially they're doing the same
thing, right? Thanks.

Have a read of http://mindprod.com/jgloss/interface.html
http://mindprod.com/jgloss/comparable.html
http://mindprod.com/jgloss/interfacevsabstract.html

When you write a proper Comparable interface you can use sort,
Collections and all sorts of other stuff that merely requires your
class implement Comparable.

If you write code that uses Comparable parameters, all kinds of things
can use your class. If you do it your way only thing of one class and
its descendants will work.
 
S

Stefan Ram

I am new to Java and I was wondering what's the difference between
implementing the Comparable interface, and just adding a "int
compareTo" method in the class? Essentially they're doing the same
thing, right?

An interface does not actually do something (at runtime). It
is a declaration of a type; and I consider the JavaDoc of the
interface to be an important part of that type declaration
giving the meaning (behavior) of the interface.

If a class implements a method

int compareTo( T o )

, it does /not/ claim that this method has a subbehavior of
the operation

compareTo(T o)

of the interface "java.lang.Comparable". To claim this, the
interface needs to be declared by "implements".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top