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.
| Sequence structure | Result | Maximum depth |
|---|---|---|
| (2+3)ร 4 | 20 | 2 |
| 2+(3ร 4) | 14 | 3 |
Chronology
A timeline of the turning points
Historical design account
Architecture-neutral bytecodes and a required runtime.
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.
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.
Continue the chronology
Related history guides
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.
- Oracle, Java SE 21Java Virtual Machine Specification, Java SE 21: selected instructions
- James Gosling and Henry McGilton / Sun Microsystems, May 1996, Oracle archiveThe Java Language Environment: architecture neutral and portable
- James Gosling, Bill Joy and Guy Steele, July 1996 / Oracle archivePreface to the first edition: Java Language Specification

