Java Arena
Step 6 of 8

The call stack

When a method calls another, Java has to remember where to come back to. It keeps a call stack: a pile of the methods that are running right now. A call puts the new method on top, with its own parameters and variables. When that method returns, it's taken off the pile, and the method below carries on right after the call.

public class Main {
    public static void main(String[] args) {
        System.out.println("main starts");
        int result = half(10);
        System.out.println("main got " + result);
    }

    public static int half(int number) {
        System.out.println("half starts with " + number);
        return number / 2;
    }
}
Output
main starts
half starts with 10
main got 5

While half runs, the stack holds half on top of main. main is paused in the middle of the line int result = half(10);, and it finishes that line with the returned 5.

You may have seen the call stack already: when a program crashes, Java prints it. Here share divides by zero:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Main.share(Main.java:8)
	at Main.main(Main.java:4)

Each at line is one method on the stack, with the line it was on, the top one first: the crash happened in share on line 8, which was called from main on line 4.

The challenges in this step are for tracing: follow the calls and the returns, and write down what's printed, in order.

Your task

Challenge 1 of 3

What does this program print?

Challenge 2

What does this program print? Three methods call each other.

Challenge 3

What does this program print? A value is returned through two calls.