一、概述
生活中来说:
父辈种地 -- 挣钱 -- 供养孩子 -- 孩子长大 -- 孩子种地 -- 孩子挣钱 -- 供养孩子 -- 孙子长大 -- 孙子种地
程序中来说:
是指在当前方法中调用自己 的这种现象
注意事项:
1.递归一定要有出口,否则会报 栈内存溢出 异常
2.递归出口完了,还是会报
代码演示:
public class Test {
public static void main (String[] args) {
// 让该方法调用100次
method();
}
// 定义一个全局count
static int count = 0;
// 定义一个方法
public static void method(){
count++;
while (count == 100){
return;
}
System.out.println("method方法执行了..." + count + "次");
method();
// 方法结束的时候继续调用自己,再来一次执行,知道满足while循环,return;之后,就不会在执行以下代码,递归就结束了
}
}
输出结果:
method方法执行了...1次