Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does javac checkcast arrays twice?

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).

like image 418
Brandon Avatar asked Mar 08 '10 19:03

Brandon


People also ask

What is LDC in Java?

ByteCode:ldc pushes a one-word constant onto the operand stack. ldc takes a single parameter, , which is the value to push. Most of the bytecodes in JVM can figure out their name by the code description.

What are byte code instructions?

A method's bytecode stream is a sequence of instructions for the Java virtual machine. Each instruction consists of a one-byte opcode followed by zero or more operands. The opcode indicates the action to take.

Who executes the bytecode in Java?

Compiler converts the source code or the Java program into the Byte Code(or machine code), and secondly, the Interpreter executes the byte code on the system. The Interpreter can also be called JVM(Java Virtual Machine).

Why bytecode is called bytecode?

The name bytecode stems from instruction sets that have one-byte opcodes followed by optional parameters.


1 Answers

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

like image 127
Thomas Pornin Avatar answered Sep 28 '22 03:09

Thomas Pornin