当前位置:网站首页>Recursive execution mechanism
Recursive execution mechanism
2022-07-05 00:26:00 【The learning path of Java Rookies】
recursive
Recursion is a method that calls itself
The rules
1. When executing a method , Just create a new protected independent space ( Stack space )
2. The local variables of the method are independent , No interaction
3. If a reference data type variable is used in the method , Will share data of this reference type
4. Recursion must have an end condition , Otherwise, there will be stack overflow
5. Recursion takes up a lot of memory
6. When a method is executed , Or meet return, It will return , Follow who calls , Just return the results to who , At the same time, when the method is completed or returned , This method is finished
The execution mechanism of recursive call
public class Recursion01 {
// Write a main Method
public static void main(String[] args) {
T t1 = new T();
t1.test(4);// What output ? n=2 n=3 n=4
}
}
class T {
// analysis
public void test(int n) {
if (n > 2) {
test(n - 1);
}
System.out.println("n=" + n);
}
}
Take the one above as an example :
First of all to enter main Method , Then open up space in the stack memory , Then perform main Code snippets in the method , When main Method call test() Method time , Will open up a new space in the stack , Then put the arguments 4 Pass in
because ,if(n>2) The judgment condition is true, So it will execute test(n-1) Method , Open up a new space in the stack , until n=2 When , The judgment is not true , At this point, the console outputs 2, then n=2 This method is over , This stack space will be released , Back to n = 3 Of this space text(n -1); Continue with the following statement , Until the end of the program .
边栏推荐
- In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
- TS快速入门-函数
- 人脸识别5- insight-face-paddle-代码实战笔记
- Verilog tutorial (11) initial block in Verilog
- How many triangles are there in the golden K-line diagram?
- 业务实现-日志写到同一个行数据里面
- 电力运维云平台:开启电力系统“无人值班、少人值守”新模式
- [error reporting] "typeerror: cannot read properties of undefined (reading 'split')“
- Advanced template
- Learning of basic amplification circuit
猜你喜欢

In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database

Illustrated network: what is gateway load balancing protocol GLBP?
![[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation](/img/41/eb790e7419a158e985fa503bd7dc17.png)
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation

How to use fast parsing to make IOT cloud platform

Learn C language from scratch day 024

电力运维云平台:开启电力系统“无人值班、少人值守”新模式

What did I pay for it transfer to testing post from confusion to firmness?

【selenium自动化】常用注解

Significance of acrel EMS integrated energy efficiency platform in campus construction

2022.07.03(LC_6108_解密消息)
随机推荐
A new method for analyzing the trend chart of London Silver
IELTS examination process, what to pay attention to and how to review?
JS convert pseudo array to array
Two numbers replace each other
两个数相互替换
AcWing164. 可达性统计(拓扑排序+bitset)
Summer challenge brings you to play harmoniyos multi terminal piano performance
URLs and URIs
uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
GDB常用命令
TS快速入门-函数
P4281 [AHOI2008]紧急集合 / 聚会(LCA)
积分商城游戏设置的基本要点
Hologres Query管理及超时处理
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Learning of basic amplification circuit
圖解網絡:什麼是網關負載均衡協議GLBP?
Hisilicon 3559 universal platform construction: YUV422 pit stepping record
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
How to effectively monitor the DC column head cabinet