Understanding Method Invocation in Java Bytecode
The article explains how Java bytecode implements method invocation by extracting a calculation into a static method, detailing the use of the invokestatic instruction, operand stack handling, constant pool references, and subsequent bytecode operations such as i2d, Math.pow, dadd, Math.sqrt, and type conversions.
This translated article, originally authored by Mahmoud Anouti, introduces the mechanics of method invocation in Java bytecode.
Starting from a simple main method, the example moves the computation of variable c into a new static method named calc , and shows the resulting bytecode.
The main method now uses the invokestatic instruction instead of iadd . Two arguments are pushed onto the operand stack before the call, and invokestatic creates a new stack frame, placing the arguments into the callee’s local variable array.
The invokestatic opcode occupies three bytes because it includes two extra bytes that form a symbolic reference to the target method (constant‑pool entry #2 for calc ).
Inside calc , the bytecode first loads the first integer argument ( iload_0 ), converts it to double with i2d , pushes the constant 2.0d , and then invokes the static method Math.pow . The two prepared operands become the arguments for Math.pow . When Math.pow returns, its result is left on the operand stack.
The same process is applied for Math.pow(b, 2) (shown in the next image).
After the dadd instruction adds the two intermediate results, another invokestatic calls Math.sqrt . The result is converted back to int with d2i and stored in c ( istore_3 ).
For further details, see the linked articles “Java Bytecode Introduction Ⅰ” and “Java Bytecode Introduction Ⅱ”.
Original content, please credit the source when reposting.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.