JVM in action
A visualization I have prepared today for our weekly team knowledge sharing session. Displayed is the Java Virtual Machine as it executes the code of a simple sum() function. I have used the javap disassembler to generate the readable bytecode instructions which are highlighted with the current instruction pointer.
The Local Variables contains the function arguments and all local variables declared in the function. Since the function is not static, the first argument is “this”.
The JVM is a stack-oriented abstract machine – if an instruction needs an input, this input should be put on the Operand Stack before calling the instruction itself.
Each JVM instruction is a single byte. For example, iload_0 means “load local variable with index 0 as int in the Operand Stack”. So the “i” in the “iload_0” means “int”. This instruction is part of a group of int operations: iload_0, iload_1, iload_2, iload_3 for which you don’t need to supply an index as it is part of the instruction itself. When you need to access Local Variable with index more than 3, you need to use the generic iload instruction and supply the index as a second byte that follows the byte of the instruction. JVM provides also lload, fload, dload, aload for the other primitive types. Each of these operations has additional 4 instructions for manipulation of the first 4 Local Variables as iload does.
Big thanks to Alexander Shopov for his inspiring presentations: https://lnkd.in/dYpgkKfK & https://lnkd.in/dZQy8S5H