当前位置:网站首页>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
- Summer challenge brings you to play harmoniyos multi terminal piano performance
- 【雅思阅读】王希伟阅读P4(matching1)
- 2022.07.03 (LC 6109 number of people who know secrets)
- Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
- P4408 [NOI2003] 逃学的小孩(树的直径)
- Distributed base theory
- The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
- [error reporting] "typeerror: cannot read properties of undefined (reading 'split')“
- 城市轨道交通站应急照明疏散指示系统设计
猜你喜欢
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
How to do the project of computer remote company in foreign Internet?
IELTS examination process, what to pay attention to and how to review?
如何有效对直流列头柜进行监测
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
Application of fire fighting system based on 3D GIS platform
华为200万年薪聘请数据治理专家!背后的千亿市场值得关注
海思3559万能平台搭建:YUV422的踩坑记录
【C】 (written examination questions) pointer and array, pointer
随机推荐
The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
【路径规划】RRT增加动力模型进行轨迹规划
Upload avatar on uniapp
Hash table, hash function, bloom filter, consistency hash
Significance of acrel EMS integrated energy efficiency platform in campus construction
P3304 [sdoi2013] diameter (diameter of tree)
Introduction to ACM combination counting
How to use fast parsing to make IOT cloud platform
【C】 (written examination questions) pointer and array, pointer
"Xiaodeng" domain password policy enhancer in operation and maintenance
(script) one click deployment of any version of redis - the way to build a dream
GDB common commands
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
P3304 [SDOI2013]直径(树的直径)
Fast analysis -- easy to use intranet security software
实战模拟│JWT 登录认证
2022.07.03 (lc_6111_counts the number of ways to place houses)
积分商城游戏设置的基本要点
基本放大电路的学习