Originally posted by Peter Chase:
Do you have any figures to show how slow it is?
It's intuitively pretty expensive because it involves constructing an Exception, which involves constructing a StackTraceElement[], which can be quite a few objects -- each of them involving a native call or two, which is itself expensive.
I just went and looked in Jack Shirazi's "Java Performance Tuning", second edition. He's got a benchmark on pages 174-175 that shows that in JDK 1.4, anyway, doing a cast that throws a ClassCastException is fully 100 times slower then just
testing with instanceof and branching.