next up previous
Next: Example: the last ones Up: Unit 11 Previous: Example of evolution of

Evolution of the stack of ARs in the case of recursive methods

In the case of recursive methods, the way in which the stack of ARs and the program counter evolve is exactly the same as for non-recursive methods. However, it is important to remember that an AR is associated to an activation of a method, and not to a method.

Example: Let us consider the following method recursive() and its activation from the main() method:

public static void recursive(int i) {
  System.out.print("In recursive(" + i + ")");
  if (i == 0)
    System.out.println(" - Finished");
  else {
    System.out.println(" - Activation of recursive("
                       + (i-1) + ")");
    recursive(i-1);
    System.out.print("Again in recursive(" + i + ")");
    System.out.println(" - Finished");
  }
  return;
}

public static void main(String[] args) {
  int j;

  System.out.print("Insert a non-negative integer: ");
  Scanner sc = new Scanner(System.in);
  j = sc.nextInt();

  System.out.println("In main - Activation of recursive(" + j + ")");
  recursive(j);
  System.out.print("Again in main");
  System.out.println(" - Finished");
}

Output generated by the program when the user inputs the value 2:

Insert a non-negative integer: 2
In main - Activation of recursive(2)
In recursive(2) - Activation of recursive(1)
In recursive(1) - Activation of recursive(0)
In recursive(0) - Finished
Again in recursive(1) - Finished
Again in recursive(2) - Finished

The evolution of the stack of ARs is shown below. we have assumed that 258 is the address of the statement that follows the activation of recursive(j) in main(), and that 532 is the address of the statement that follows the activation of recursive(i-1) in recursive(). Since the invoked methods do not return any value (the return type is void), the ARs do not contain a memory location for such a value. Moreover, we have not indicated to which method each AR refers, since the AR at the bottom of the stack is for the main() method, and all the others are for the successive activations of recursive().

% for latex2html
\begin{tabular}{@{}ccccccc@{}}
\begin{tabular}[b]{\vert r\vert ...
...ine
\multicolumn{2}{c}{\large\raisebox{0mm}[5mm]{7}}
\end{tabular}\end{tabular}

Note that, for the various recursive activations, different ARs are created on the stack, with successively decreasing values of the parameter i, up to the last recursive activation, for which the parameter i has value 0. In the latter case, no further recursive call is made, the string " - Finished" is printed, and the activation terminates. This causes the previous activations to print the message "Again in recursive(i) - Finished", and to terminate.

Also note that the Java bytecode associated to the various recursive activations is always the same, i.e., the one of the method recursive(). Hence, the return address stored in the AR for the various recursive activations is always the same (namely 532), except for the first activation, for which the return address is that of a statement in the main() method (namely 258).


next up previous
Next: Example: the last ones Up: Unit 11 Previous: Example of evolution of