Inspecting JVM Bytecode with javap & Bytecode Opcodes
1Concept
JVM bytecode is a set of zero-operand stack machine instructions. Opcodes like `iload`, `istore`, `iadd`, `invokevirtual`, `invokestatic`, and `areturn` manipulate the execution stack during bytecode execution.
2Architecture Diagram
Stack Machine Operand Stack:
[ Push 10 ] ---> [ Push 20 ] ---> [ Exec iadd ] ---> [ Pop Result 30 ]
|10| |20| | | |30|
+--+ |10| +--+ +--+
+--+3Code Example
Core Java
public class BytecodeInspection {
public int computeFactorial(int n) {
if (n <= 1) return 1;
return n * computeFactorial(n - 1);
}
public static void main(String[] args) {
BytecodeInspection demo = new BytecodeInspection();
System.out.println("Factorial 5: " + demo.computeFactorial(5));
}
}4Expected Output
Factorial 5: 120
5Key Takeaways
- ✓Run 'javap -c -v BytecodeInspection' to inspect constant pool and assembly opcodes.
- ✓JVM stack frames hold local variables, operand stack, and frame data.
- ✓Understanding bytecode enables deep troubleshooting of proxy classes and byte-manipulation tools (ASM/ByteBuddy).