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