Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > why use Comparable interface?

Reply
Thread Tools

why use Comparable interface?

 
 
jesssi1203@yahoo.com
Guest
Posts: n/a
 
      09-19-2005
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.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-19-2005
wrote:
> 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

 
Reply With Quote
 
 
 
 
john@wezayzo.com
Guest
Posts: n/a
 
      09-19-2005
> 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.

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      09-19-2005
wrote:

> 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



 
Reply With Quote
 
Eric K Idema
Guest
Posts: n/a
 
      09-19-2005
wrote:
> 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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-20-2005
On 19 Sep 2005 05:06:40 -0700, wrote or quoted :

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


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      09-20-2005
> 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".

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why aren't OrderedDicts comparable with < etc? Mark Summerfield Python 17 07-25-2009 06:18 PM
Discussion of why java.lang.Number does not implement Comparable Sideswipe Java 42 08-02-2007 02:24 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why doesn't Array include Comparable ara.t.howard@noaa.gov Ruby 7 08-09-2006 02:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57