当前位置:网站首页>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 .
边栏推荐
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- GDB常用命令
- Consolidated expression C case simple variable operation
- Using the uniapp rich text editor
- Summer challenge brings you to play harmoniyos multi terminal piano performance
- [paper reading] cavemix: a simple data augmentation method for brain vision segmentation
- 《论文笔记》Multi-UAV Collaborative Monocular SLAM
- 【雅思阅读】王希伟阅读P4(matching1)
- 【雅思阅读】王希伟阅读P3(Heading)
- 雅思考试流程、需要具体注意些什么、怎么复习?
猜你喜欢

Using the uniapp rich text editor

【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)

雅思考试流程、需要具体注意些什么、怎么复习?

The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!

1189. Maximum number of "balloons"

Deux nombres se remplacent

Design of emergency lighting evacuation indication system for urban rail transit station

海思3559万能平台搭建:YUV422的踩坑记录

【雅思阅读】王希伟阅读P3(Heading)

Hash table, hash function, bloom filter, consistency hash
随机推荐
【北京大学】Tensorflow2.0-1-开篇
Summer challenge brings you to play harmoniyos multi terminal piano performance
海思3559万能平台搭建:YUV422的踩坑记录
[path planning] RRT adds dynamic model for trajectory planning
Upload avatar on uniapp
URL和URI
js如何实现数组转树
What did I pay for it transfer to testing post from confusion to firmness?
《论文笔记》Multi-UAV Collaborative Monocular SLAM
华为200万年薪聘请数据治理专家!背后的千亿市场值得关注
Hash table, hash function, bloom filter, consistency hash
Application of multi loop instrument in base station "switching to direct"
Hologres Query管理及超时处理
基本放大电路的学习
【雅思阅读】王希伟阅读P3(Heading)
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
Microservice
人脸识别5- insight-face-paddle-代码实战笔记
XML的解析
Learning of basic amplification circuit