An example of how the JVM will execute this.
Java code:
When I disassemble Test1 and Test2 using "java -c" I get the following:
Both include the same instructions, only in different order. Note that checkcast will do the actual checking. If I would leave out the call to Test.doIt() then class Test2 still has the checkcast whereas class Test1 does not. So yes, it does lead to a little more speed if you minimize the number of casts by using the most often used type as the first one.
That said, that is really nano optimization. The speed increase is so minimal I can't even call it micro optimization. Even stranger, a little
test I've done shows that the code
with the check is faster:
Output:
18072362
16765182
On average, each call is less than 20 nanoseconds for both calls. The difference is less than 3 nanoseconds. I wouldn't bother changing anything for these differences.