跪拜 Guibai
← All articles
Backend · Java · JVM

Swing Operand Stack Demo: Pick a Method, Change Arguments, Watch Real Bytecode Run

By 金銀銅鐵 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The demo turns abstract JVM stack rules into a visible, step-by-step process, and it respects the boundary between compile-time and runtime by using real javac output. That distinction matters for anyone trying to understand how bytecode actually executes.

Summary

The demo is a Java Swing rewrite of an earlier Python/Tkinter tool for visualizing how JVM bytecode instructions shift values on the operand stack. Instead of letting users click instruction buttons to build arbitrary sequences, which implies bytecode can be assembled at runtime, the new version exposes 11 methods actually compiled by javac 21. Users pick a method, edit its arguments, and step through the real decoded instruction stream; each step updates a frame view showing the local variable table and the operand stack drawn from bottom to top, with depth and max_stack displayed.

The tool covers iconst_<i>, iadd, and isub, and it makes the isub operand order explicit: the top of stack is value2, the slot below is value1, and the result is value1 - value2. It also illustrates compile-time vs runtime boundaries: the instruction sequence and max_stack are baked in, only argument values vary. The demo uses real javap output, so the byte arrays, stack depths, and return values all match what a compiler actually generates.

Takeaways
— The demo is a Swing rewrite of an earlier Python/Tkinter tool, requiring JDK 16+ (uses record) and tested with JDK 21.
— It offers 11 methods (addM1 through add5, sub1 through sub3, and calc) compiled by javac 21.
— Each addN/subN method uses a four-instruction skeleton: iload_1, iconst_N, iadd/isub, ireturn.
— For add3, the bytecode is 1B 06 60 AC; for sub3, it's 1B 06 64 AC.
— calc has six instructions and a max_stack of 3 because three iloads push all arguments before arithmetic.
— In isub, value1 is second-from-top and value2 is top; result is value1 - value2, so 2 - 7 = -5, not 7 - 2 = 5.
— max_stack is compile-time determined and shown as depth N / M in the stack panel; addN/subN have M=2, calc has M=3.
Conclusions

Switching from instruction assembly to method selection forces users to see that the instruction stream is fixed at compile time, while only arguments vary.

Basing the demo on real javap output makes the visualization a faithful mirror of compiler output, so the stack changes are not an approximation but the actual effect of those bytes.

The isub order trap is a great teaching moment: changing arguments from (2,3,4) to (7,2,0) flips the sign without any bytecode change, which makes the stack order rule stick.

Displaying max_stack ties the abstract Code attribute to observable behavior, helping learners connect javap's stack=N to the depth they see step by step.

Concepts & terms
Operand stack
A per-method LIFO stack in the JVM where bytecode instructions push and pop values. The demo draws it from bottom to top and shows its current depth relative to max_stack.
iconst_<i>
A family of seven bytecode instructions that push small integer constants from -1 to 5. The constant is encoded in the opcode itself, so the instruction takes no operand and is one byte long.
iadd / isub
Bytecode instructions that pop two ints from the operand stack and push back their sum or difference. In isub, the top of stack is value2 and the next slot is value1; the result is value1 - value2.
max_stack
A value in the Code attribute of a method that records the maximum operand stack depth needed. It is computed at compile time and does not change at runtime; the demo shows it as the M in 'depth = N / M'.
javap
A JDK tool that disassembles class files into a readable listing of bytecode instructions, including stack and locals information. The demo's instruction panel is based on its output.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗