CoursesJVM internals for Kotlin and Android developers

Puzzle 1 · 2 of 5

What does a method receive?

Track one number across a call and back.

All puzzles in this course

The problem

Look at the increment instructions in the class file from the previous puzzle. The caller holds seven. A helper adds one and returns eight. Where did the seven go while the helper ran?

See it

What does a method receive? Local 7 Stack 7, 1 Stack 8 Return 8

Scroll sideways to see the full diagram.

  1. The called method receives a copy of seven.

    Step 1 of 4
  2. The load and constant instructions supply two inputs.

    Step 2 of 4
  3. Addition replaces both inputs with one result.

    Step 3 of 4
  4. The result returns to the caller.

    Step 4 of 4

The building block

A stack frame belongs to one method call. It holds local variables and an operand stack for intermediate values. Calling increment copies the integer argument into its local slot. Inspect increment with javap. The load instruction pushes seven. The next instruction pushes one. Addition consumes both values and pushes eight. The integer return sends eight to the caller and the helper frame is discarded. The original local in the caller still holds seven.

A local is a slot belonging to the current call. The operand stack is its scratch space. In the table the rightmost number is the top, where an instruction takes its next input. The caller passes seven to a static helper, so the helper's first local slot starts at seven.

Helper instructionActionHelper stack afterwards
Before the first instructionReceive seven in local slot zeroempty
iload_0Copy the value in slot zero onto the stack7
iconst_1Push a constant one7, 1
iaddRemove the two numbers and push their sum8
ireturnDeliver eight to the caller and finish this callframe discarded

Notice that loading a local copies its value. If the caller throws away the returned eight and calls the helper again with its original seven, it gets eight again. If it passes the returned eight as the next argument, it gets nine. Trace both paths by drawing a fresh row for each call. This is the execution model described by the instructions. A runtime may optimize how it implements that model.

Try it

Run the browser experiment

Optional local lab

Complete experiment 2 in the JVM source files. Record your prediction before running the program.

Check yourself

After the addition and before the return, what is on the helper stack?

Right. Each method invocation has its own frame with locals and an operand stack.

Not quite. Addition consumes both of its inputs. Re-read that part ↑

The caller ignores the result and calls the helper again with its original seven. What comes back?

Not quite. No value was assigned to the caller's original local. Re-read that part ↑

Right. The second invocation receives seven in a fresh local slot.

To get nine on the second call, which number must it receive?

Not quite. The helper adds exactly one to the value it receives. Re-read that part ↑

Right. Passing the result explicitly connects two otherwise independent calls.

Sources

What you now know

Each method invocation has its own frame with locals and an operand stack.