Java Code:
public class SimpleRecursion {
public int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
}
Gives the following bytecode for the factorial method (I executed javap to generate it):
public int factorial(int); descriptor: (I)I flags: ACC_PUBLIC Code: stack=4, locals=2, args_size=2 0: iload_1 1: ifne 6 4: iconst_1 5: ireturn 6: iload_1 7: aload_0 8: iload_1 9: iconst_1 10: isub 11: invokevirtual #2 // Method factorial:(I)I 14: imul 15: ireturn LineNumberTable: line 4: 0 line 5: 4 line 7: 6 StackMapTable: number_of_entries = 1 frame_type = 6 /* same */
I understand that in the fifth line in the block above, stack=4 means that the stack can have at most 4 objects.
But how does the compiler compute that?