Chapter 9 · Simulation

Method Call Stack

← Chapter 9
Java code
    Call stack
    Output (console)

    Method Call Stack

    Purpose: see what happens when a method is called: a new frame is pushed onto the call stack, the values of the arguments are copied into the parameters, and local variables live in that frame; with return a value goes back to the caller and the frame is popped.

    How to use it

    1. Pick a program on the left and change the arguments.
    2. Step ▶ / ◀ Back (keyboard → / ←), Play, Reset. On the right the stack grows from bottom to top: main at the bottom, the method currently running at the top (highlighted). The header of each frame shows the return address (the line where the call was made).
    3. Try it yourself: predict the return value, the overloaded method the compiler will choose, and the output.

    Reminders

    • Java passes primitive types by value: changing a method parameter does not change the caller's variable (increment, the swap trap).
    • Local variables are visible only in the block where they are declared; they disappear when the block ends.
    • In overloading, the compiler picks the signature at compile time by looking at the number and types of the arguments; two methods that differ only in return type are not allowed.

    The output of every program was checked against real Java (JDK 21) output for every argument combination.

    Related slides: Chapter 9 · Methods (call stack, pass by value, the swap trap, method overloading, local scope, lcm).