当前位置:网站首页>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 .
边栏推荐
- TS快速入门-函数
- P4281 [AHOI2008]紧急集合 / 聚会(LCA)
- Acwing164. Accessibility Statistics (topological sorting +bitset)
- 兩個數相互替換
- Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
- URL和URI
- How to effectively monitor the DC column head cabinet
- 2022.07.03(LC_6109_知道秘密的人数)
- 分布式BASE理论
- Paper notes multi UAV collaborative monolithic slam
猜你喜欢

Application of fire fighting system based on 3D GIS platform
Date time type and format in MySQL

Using fast parsing intranet penetration to realize zero cost self built website
![[selenium automation] common notes](/img/d3/6699792e85b5ee5a2d6192f4e4d07c.png)
[selenium automation] common notes

微服务(Microservice)那点事儿

Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...

Face recognition 5- insight face padding code practice notes
![P3304 [SDOI2013]直径(树的直径)](/img/5c/984675bf4517481f80f54657c6c7ad.png)
P3304 [SDOI2013]直径(树的直径)

Hisilicon 3559 universal platform construction: YUV422 pit stepping record

【雅思阅读】王希伟阅读P3(Heading)
随机推荐
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
圖解網絡:什麼是網關負載均衡協議GLBP?
初识ROS
Learn C language from scratch day 024
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
业务场景功能的继续修改
Design of emergency lighting evacuation indication system for urban rail transit station
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
【selenium自动化】常用注解
基于三维gis平台的消防系统运用
How many triangles are there in the golden K-line diagram?
Using fast parsing intranet penetration to realize zero cost self built website
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
Acrel-EMS综合能效平台在校园建设的意义
TS快速入门-函数
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
Hologres query management and timeout processing
Get to know ROS for the first time
How to effectively monitor the DC column head cabinet
Is it safe to open an account in the College of Finance and economics? How to open an account?