casting and ClassCastException

Tom Tromey tromey@cygnus.com
Tue Jul 25 15:35:00 GMT 2000


Oskar>    public int compareTo(Object o) throws ClassCastException
Oskar>    {
Oskar>     if (o instanceof Byte)
Oskar>       return this.value - ((Byte) o).value;
Oskar>     else
Oskar>       throw new ClassCastException();
Oskar>     }

Oskar> My question is: why throw ClassCastException when that is
Oskar> automaticly done if you cast right away, like this:

I don't know why.  There might not be a reason.
Classpath does "return compareTo ((Byte) o)", which I like even more,
at least for final classes (then the call is inlineable).

Tom


More information about the Java mailing list