Java 代码:
public class SimpleRecursion {
public int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
}
为阶乘方法提供以下字节码(我执行 javap 来生成它):
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 */
我知道在上面块的第五行中,stack=4 意味着堆栈最多可以有 4 个 objects。
但是编译器是如何计算的呢?
12345678_0001
相关分类