当前位置:网站首页>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 .
边栏推荐
- Fast analysis -- easy to use intranet security software
- Distributed base theory
- Significance of acrel EMS integrated energy efficiency platform in campus construction
- Detailed explanation of openharmony resource management
- 2022.07.03(LC_6109_知道秘密的人数)
- [paper reading] cavemix: a simple data augmentation method for brain vision segmentation
- Illustrated network: what is gateway load balancing protocol GLBP?
- The pit of sizeof operator in C language
- Cross domain request
- npm install报错 强制安装
猜你喜欢
lambda表达式
How to do the project of computer remote company in foreign Internet?
OpenHarmony资源管理详解
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
js如何实现数组转树
城市轨道交通站应急照明疏散指示系统设计
Paper notes multi UAV collaborative monolithic slam
Pytoch --- use pytoch to realize linknet for semantic segmentation
JS how to realize array to tree
PMP certificate renewal process
随机推荐
【雅思阅读】王希伟阅读P4(matching1)
TS quick start - functions
Upload avatar on uniapp
巩固表达式C# 案例简单变量运算
[IELTS reading] Wang Xiwei reading P4 (matching1)
XML的解析
IT转测试岗,从迷茫到坚定我究竟付出了什么?
2022.07.03(LC_6109_知道秘密的人数)
ORB(Oriented FAST and Rotated BRIEF)
基于三维gis平台的消防系统运用
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Get to know ROS for the first time
Summary of week 22-07-02
IELTS examination process, what to pay attention to and how to review?
兩個數相互替換
Enterprise application business scenarios, function addition and modification of C source code
多回路仪表在基站“转改直”方面的应用
Summer challenge brings you to play harmoniyos multi terminal piano performance
[IELTS reading] Wang Xiwei reading P3 (heading)
GDB常用命令