Skip to main content
Engineering LibreTexts

13.9: Recursive Stack Diagrams

  • Page ID
    18823
  • In the previous chapter, we used a stack diagram to represent the state of a program during a method invocation. The same kind of diagram can make it easier to interpret a recursive method.

    Remember that every time a method gets called, Java creates a new frame that contains the current method’s parameters and variables. Figure 5.9.1 is a stack diagram for countdown, called with n == 3.

    Stack diagram for the countdown program.
    Figure \(\PageIndex{1}\): Stack diagram for the countdown program.

    By convention, the stack for main is at the top and the stack grows down. The frame for main is empty because main does not have any variables. (It has the parameter args, but since we’re not using it, we left it out of the diagram.)

    There are four frames for countdown, each with a different value for the parameter n. The last frame, with n == 0, is called the base case. It does not make a recursive call, so there are no more frames below it.

    If there is no base case in a recursive method, or if the base case is never reached, the stack would grow forever, at least in theory. In practice, the size of the stack is limited; if you exceed the limit, you get a StackOverflowError.

    For example, here is a recursive method without a base case:

    public static void forever(String s) {
        System.out.println(s);
        forever(s);
    }
    

    This method displays the string until the stack overflows, at which point it throws an exception.

    • Was this article helpful?