Why are javac chancava arrays twice?

Studying bytecode, I noticed that javac seems to duplicate checkcast instructions when casting to array types.

 Cast.java: class Cast { void test(Object a) { Object[] b = (Object[])b; } } 

javap disassembler compiled javac version

 void test(java.lang.Object); Code: 0: aload_1 1: checkcast #2; //class "[Ljava/lang/Object;" 4: checkcast #2; //class "[Ljava/lang/Object;" 7: astore_2 8: return 

Testing jikes shows expected single throw

 void test(java.lang.Object); Code: 0: aload_1 1: checkcast #10; //class "[Ljava/lang/Object;" 4: astore_2 5: return 

checkcast should checkcast an exception if the object cannot be considered the requested type and otherwise does nothing, so I don’t understand why this can help double the action. I did not look at the JDK sources to see how this happens, and if it helps explain why (perhaps it meant as a hint).

+6
jvm bytecode javac jikes
source share
1 answer

This is a known javac bug . But it is basically harmless.

+8
source share

All Articles