Unequal Times

Posted by Mike Haller on Friday, January 11. 2008 at 18:37 in Java
Suppose you've got the following code:

if ((o1 instanceof Comparable)
 && (o2 instanceof Comparable))
{
   // Throws ClassCastException
   return ((Comparable) o1).compareTo((Comparable) o2);
}


Do you think compareTo() throws a ClassCastException?

Well, it does in the following case:
o1 is a javax.sql.Timestamp and o2 is a java.util.Date

Since Timestamp subclasses Date, this is a little bit confusing.

Update: I probably had an old installation of JRE (e.g. JRE < 1.5.0_06), as this bugs seems to be fixed in 1.5.0_07, see Sun Bug 5103041. This is fixed in the JRE I use at home 1.5.0_013. Thanks to my collegue Lars for pointing that out.


Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications
 
Submitted comments will be subject to moderation before being displayed.
 

About

My name is Mike Haller and I'm a software developer and architect at Bosch Software Innovations in Germany. I love programming, playing games and reading books. I like good food, making photos and learning and mentoring about the craftsmanship of commercial software development. Stack Overflow profile for mhaller

Quicksearch