Question

Examining bytecode, I've noticed javac seems to duplicate checkcast instructions when casting to array types.

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

javap disassembly of the javac compiled 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 the expected single cast

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

checkcast is supposed to raise an exception if the object can't be treated as the requested type and otherwise does nothing, so I don't see why it might help to double the cast. I haven't looked at the JDK sources to see how it's produced, and if that helps explain the why (maybe it's meant as a hint).

Was it helpful?

Solution

It is a known bug of javac. But it is mostly harmless.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top