[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

comparable interface and compareTo method

Posted on 2006-04-30
4
Medium Priority
?
512 Views
Last Modified: 2008-03-10
You use the compareTo method to compare two objects right? How come sometimes before comparing the objects it is necessary to type cast them to type Comparable?  (Comparable)obj
0
Comment
Question by:yattias
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
BogoJoker earned 240 total points
ID: 16575145
Hi yattias,

Could you show the situation where this is happening for you?

If you accept the object as:
Object obj;
Then you know nothing of that object's methods but the most basic.  You are uncertain if that has a compareTo() method at all.  This can be improved with generics.  Ensure that the object must extend Comparable and then the compiler would already know that hte object has a compareTo method.  Same if instead of doing Object obj you did Comparable obj.  Just if it was defiend as an Object then the compiler does not know any information about that object except the basics.

Joe P
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16575156
http://www.developer.com/java/article.php/858411

Just check out all the 3 parts.

I don't think that kind of Type casting is required as many classes already implement if for exacmple check out String Class it a Comparable Object.
0
 

Author Comment

by:yattias
ID: 16575162
thanks, what I meant was really in general, why would you type cast an object to Comparable? is it so it will include the comparable interface for incase it does not?
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16575174
> why would you type case an object to Comparable
To ensure that the object your casting has a compareTo method.

Look at the Object class definition.  It has what, 10 methods?
wait, clone, toString, notifyAll, notify, hashCode, getClass, finalize, equals

None of those are compareTo.  If you do this:
Object obj = new Object();
obj.compareTo(new Object());

Then the compiler will say I don't see a compareTo method.  You have a problem!
Type casting it to a Comparable ensures the compiler that the compareTo is defined in the object.
Joe P
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question