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).
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.
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.
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).
The name bytecode stems from instruction sets that have one-byte opcodes followed by optional parameters.
It is a known bug of javac. But it is mostly harmless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With