Swing Operand Stack Demo: Pick a Method, Change Arguments, Watch Real Bytecode Run
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.
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.
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.