aspose file tools
The moose likes Beginning Java and the fly likes Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Comparable" Watch "Comparable" New topic
Author

Comparable

Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
Hallo,

Could someone explain the difference why it happens. And what this Comparable interface is used for anyway?
I read the documentation, but i got one more question instead of clearifying: what is a natural ordering.
Any help appreciated.
Mark Mokris
Ranch Hand

Joined: May 08, 2002
Posts: 61
Comparable is an interface defined in java.lang. That is key. So if you write:
Comparable e = 2;
You are declaring a reference variable of type Comparable and assigning a primative data value (in this case an int literal) to it. This doesn't make any sense, and the compiler recognizes it, giving you an error. Under no circumstances can you assigned a primitive data value to an object reference variable.
In the second case:
Comparable e1 = new Integer(2);
You declare the reference variable but assign an Integer object to it. This is okay. You can assign an object to an interface reference variable as long as the object's class implements that interface somewhere up the hierarchy, which the Interger wrapper class happens to do.
You probably can show some valid reason for doing this, but out of context, it doesn't make a whole lot of sense (at least not to me).
Mark
Mark Mokris
Ranch Hand

Joined: May 08, 2002
Posts: 61
Your last question. The Comparable interface can be used to override the way that objects are sorted when maintaining an ordered data structure using one of the java.util.Collections classes. If you don't use the Comparable interface, the objects are ordered in the natural order. For example, Integer objects are sorted in ascending numerical order. The more appropriate term I believe is "default order".
If you want to sort Integers descending, for example, you can subclass Integer and implement the Comparable interface. This supplies a method called compareTo(). You provide YOUR OWN implementation of compareTo(), thus causing objects to be sorted the way YOU want them to.
I have never done this, and I hope I am not steering you wrong.
Mark
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Mark Mokris:
Your last question. The Comparable interface can be used to override the way that objects are sorted when maintaining an ordered data structure using one of the java.util.Collections classes. If you don't use the Comparable interface, the objects are ordered in the natural order. For example, Integer objects are sorted in ascending numerical order. The more appropriate term I believe is "default order".
If you want to sort Integers descending, for example, you can subclass Integer and implement the Comparable interface. This supplies a method called compareTo(). You provide YOUR OWN implementation of compareTo(), thus causing objects to be sorted the way YOU want them to.

Mark, you are confusing Comparable and Comparator.
A Comparable defines a compareTo(Object) method, and is meant to be used to implement the "natural" ordering of a class. That is why Integer implements Comparable.
A Comparator has a compare(Object, Object) method and is used to override the default comparing as described by you above.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
thank you for a quick replies.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Comparable
 
Similar Threads
Collections.sort() API syntax
Diff. Between Comparable & Comparator Interface
generics
Generics Warnings
Comparing Comparable