当前位置:网站首页>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 .
边栏推荐
- Summary of week 22-07-02
- [selenium automation] common notes
- TS快速入门-函数
- How to save your code works quickly to better protect your labor achievements
- 同事的接口文档我每次看着就头大,毛病多多。。。
- abc 258 G - Triangle(bitset)
- In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
- 电力运维云平台:开启电力系统“无人值班、少人值守”新模式
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- Deux nombres se remplacent
猜你喜欢

Fast parsing intranet penetration helps enterprises quickly achieve collaborative office

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

如何有效对直流列头柜进行监测

Using the uniapp rich text editor

How to avoid arc generation—— Aafd fault arc detector solves the problem for you

Using fast parsing intranet penetration to realize zero cost self built website

"Xiaodeng" domain password policy enhancer in operation and maintenance
![[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])](/img/83/63296108b47eda37c19b9ff9deb5ec.png)
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])

Continuous modification of business scenario functions

lambda表达式
随机推荐
[path planning] RRT adds dynamic model for trajectory planning
Huawei employs data management experts with an annual salary of 2million! The 100 billion market behind it deserves attention
[Peking University] tensorflow2.0-1-opening
How many triangles are there in the golden K-line diagram?
Is it safe to open an account in the College of Finance and economics? How to open an account?
Business implementation - the log is written to the same row of data
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
Five papers recommended for the new development of convolutional neural network in deep learning
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
Consolidated expression C case simple variable operation
Cross domain request
基本放大电路的学习
Fast analysis -- easy to use intranet security software
Enterprise application business scenarios, function addition and modification of C source code
兩個數相互替換
How to use fast parsing to make IOT cloud platform
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Verilog tutorial (11) initial block in Verilog
模板的进阶
AcWing164. 可达性统计(拓扑排序+bitset)