Research and learning

1996 / Java SE 21

JVM Stack Traces: Follow the Operands

JVM instruction semantics support an original bytecode trace. Follow a small stack calculation and distinguish an abstract machine from a physical processor.

Original three-panel learning illustration: JVM Stack Traces: Follow the Operands
20
Result of the invented instruction sequence
2
Maximum operand-stack depth in the invented trace

The short answer

Keep the top of the stack on the right. Write each intermediate state before evaluating the next instruction; this makes a plausible final answer auditable.

An explicit record and model comparison

Invented arithmetic, explicit order

These rows describe the toy sequences, not observed compiled files.

Top of stack at right; integer values in the stated small range.
Sequence structureResultMaximum depth
(2+3)ร— 4202
2+(3ร— 4)143

Chronology

A timeline of the turning points

  1. Historical design account

    Architecture-neutral bytecodes and a required runtime.

  2. Specified semantics

    Selected instruction definitions.

A historical objective and a precise edition

The 1996 language account distinguishes bytecodes for a hypothetical machine from native hardware instructions and conditions portability on the runtime.

The Java SE 21 specification defines iconst, iadd, imul and ireturn through operand-stack effects.

The first record explains a design objective; the second lets a reader follow particular operations. The edition label matters. This guide is not silently updating a historical white paper with every feature of a later platform.

Sources for this section: [2] ยท [1]

The invented instruction sequence

Use a method returning int, an initially empty operand stack, and the sequence: iconst_2; iconst_3; iadd; iconst_4; imul; ireturn. There are no branches or method calls within this toy sequence. Treat each number as an int and ignore unrelated synchronisation issues.

List the stack after each instruction, calculate the largest stack depth, and state which value reaches the caller. Do not infer source-language parentheses from a guessed result; derive the result from the operations actually listed.

Sources for this section: [1]

Reference answer

The stacks are [2], [2,3], [5], [5,4], [20], then empty in the returning method. The returned value is 20. The largest depth is two values, reached twice. Addition happens before multiplication because that is the instruction order.

If the sequence instead pushes 2, 3 and 4 before multiplying and then adding, the result is 14 and the maximum depth is three. That alternative is an invented comparison, not a compiler output observed here. Parentheses and operand availability matter even when the same numbers appear.

Sources for this section: [1]

The boundary of the trace

For int addition and multiplication, the specification retains the low 32 bits; overflow does not itself throw an exception.

The chosen small inputs avoid that complication. The guide neither benchmarks a physical CPU nor proves all Java programs portable. It traces selected abstract instructions. Library availability, native interfaces and the rest of an application need additional evidence.

Sources for this section: [1] ยท [2]

Continue the chronology

Biography context

Explore United States through connected lives

Connected biographies

A career behind the record

Quick answers

Common questions

Was a class file executed?
No. The answer is a hand trace checked against selected instruction definitions.
Is the virtual machine the same thing as a CPU?
No. The trace specifies abstract operations; it supplies no particular hardware implementation or timing result.

Primary and institutional sources

How this history was checked

Selected original records and contextual histories are identified below. Invented examples are teaching models, not reconstructed experiments. This article was reviewed on 31 October 2026.

  1. Oracle, Java SE 21Java Virtual Machine Specification, Java SE 21: selected instructions
  2. James Gosling and Henry McGilton / Sun Microsystems, May 1996, Oracle archiveThe Java Language Environment: architecture neutral and portable
  3. James Gosling, Bill Joy and Guy Steele, July 1996 / Oracle archivePreface to the first edition: Java Language Specification