This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes Cast to Comparable<T> ??? - Can someone explain? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cast to Comparable<T> ??? - Can someone explain?" Watch "Cast to Comparable<T> ??? - Can someone explain?" New topic
Author

Cast to Comparable<T> ??? - Can someone explain?

James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Hi ladies/gents,

It's been a while and I'm looking at returning to grad school next semester, so I've dug out the DS&A book. One particular thing I don't understand:

This is a generic method to be used with an ordered array-based list to search for a good insertion point for an object. I totally follow the logic of the algorithm, but the line:

Comparable<T> temp = (Comparable<T>) list[loc]

really gets me. Comparable is an interface, is it not? If so, how can you then successfully compare this object that was cast to Comparable<T> if the method compareTo() is not implemented (interfaces are classes with no implemented methods, correct?)? I don't get my book's explanation:

'note that list[loc] is a reference of the Object type which was type cast to T. Now the class Object does not contain the method compareTo(). So we first create a reference of the type Comparable<T> and then use the compareTo() method. Also note that this method would work on the objects of only those classes that implement the interface Comparable and provide an appropriate definition of the method compareTo()'

OK, so, if this class doesn't implement the compareTo() method, and the Comparable<T> interface doesn't, then, following the above explanation ('this method would work on the objects of only those classes that implment the interface Comparable AND provide and appropriate definition of the method compareTo()' ), this shouldn't work, right? Or am I not looking at it correctly?

I remember really disliking this class when I took it, and I think it was becuase I just tried to trudge through it myself and didn't ask enough questions. I'm determined to understand the material inside-out this time, so any help with an explanation would be greatly appreciated.

Method:


Hello. My name is Inigo Montoya. You killed my father. Prepare to die.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Hi Patrick,

Imagine you have a paper bag in your hand. You're in a darkened room. You can feel that the bag is heavy; there's something in it. Furthermore, you can feel that it's full of lots of little things, all the same size. But you don't know what.

Now I tell you, "the bag is full of peanut M&Ms." You say "cool!," grab a handful, and toss them into your mouth. Next, one of two things happens:

1) You bite into a delicious handful of peanut M&Ms.
2) You break all your teeth because the bag is actually full of marbles. Ouch.

OK, why am I telling you this?

a) The bag is an "array of Object".
b) My telling you "the bag contains peanut M&Ms" is casting the bag's contents
c) The broken teeth is what happens if the cast is wrong!!

Actually in Java, you don't even get that far. Casts are checked, so the line in question will throw an exception if the cast is incorrect. The code will work fine if, and only if, the cast is correct -- i.e., if the array really does contain objects that implement Comparable<T>. Otherwise it will fail. Simple as that.


[Jess in Action][AskingGoodQuestions]
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Ernest Friedman-Hill wrote:Hi Patrick,

Imagine you have a paper bag in your hand. You're in a darkened room. You can feel that the bag is heavy; there's something in it. Furthermore, you can feel that it's full of lots of little things, all the same size. But you don't know what.

Now I tell you, "the bag is full of peanut M&Ms." You say "cool!," grab a handful, and toss them into your mouth. Next, one of two things happens:

1) You bite into a delicious handful of peanut M&Ms.
2) You break all your teeth because the bag is actually full of marbles. Ouch.

OK, why am I telling you this?

a) The bag is an "array of Object".
b) My telling you "the bag contains peanut M&Ms" is casting the bag's contents
c) The broken teeth is what happens if the cast is wrong!!

Actually in Java, you don't even get that far. Casts are checked, so the line in question will throw an exception if the cast is incorrect. The code will work fine if, and only if, the cast is correct -- i.e., if the array really does contain objects that implement Comparable<T>. Otherwise it will fail. Simple as that.


Thanks for the response! Let me be more specific: what I don't understand is the line:

Comparable<T> temp = (Comparable<T>) list[loc]

It looks as if we are casting the object located at list[loc] to an object of type 'Comparable'. What is this? Is 'Comparable' a type of object, as well as an interface? Objects can't be instantiated from an interface, correct? If not, how can we have a 'Comparable' object? I guess this is what I don't understand. Thanks again!
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
You can cast an object reference to an interface reference as long as the object implements the referenced interface.

I think that's what's tripping you up. Or is it the <T>?


SCJA
~Currently preparing for SCJP6
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
To go off of Ernest's example, instead of telling you the bag was full of peanut M&M's were just telling you it's full of "Edibles" or "things that can be eaten". The same scenario plays out as before but an "Edible" is not a solid object, it's a concept or a description of an object. The peanut M&M is still edible so it can be safely cast to one even though edible is an interface.

HTH
James Brooks
Gunslinger
Ranch Hand

Joined: Aug 17, 2006
Posts: 165
Brian Legg wrote:You can cast an object reference to an interface reference as long as the object implements the referenced interface.

I think that's what's tripping you up. Or is it the <T>?


Ah-hah, thank you, sir. Yes, that's exactly what I wanted to know. So, really, even thought the code compiles, you could get in trouble by not watching what you pass into the method. The onus, then, is on the programmer to make sure that anything passed into the method implements compareTo() in this case, right?
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Yes and no, you should know if what your passing implements Comparable or not but if you make a mistake the compiler will stop you with a warning before you get further. That's the great thing about defining what type of objects a collection holds. I couldn't tell you for sure with you code though as it's incomplete. Your code doesn't show how or where length and list are defined.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cast to Comparable<T> ??? - Can someone explain?
 
Similar Threads
Date comparision problem
Sorting an ArrayList
Comparable<T> - must override compareTo()???
Simple J2SE 5.0 Tiger Notes
compareTo method