Home > Java > javaTutorial > body text

What is the execution process of java method calls in memory?

PHPz
Release: 2023-04-18 17:13:03
forward
1683 people have browsed it

1. Premise

1. Stack memory schematic diagram

What is the execution process of java method calls in memory?

2. How does the JVM run methods???

  • 1. During the running phase, the classLoader class loader will put the code fragment corresponding to the method in the class file into the method area in the memory area so that it can be used the next time the method is called. this method.

  • 2. During the running process, the JVM will push the code blocks in the method into the stack space in the memory according to the order of method calls. Use the execution order of statements (from top to bottom, from inside to outside) to perform stack popping and running operations

2. Call (execution) process

1. Rules for method calls

Method

When calling for the first time, the corresponding code block of the method will be loaded into the memory method area through the class loader

Every time the method is called It is equivalent to a

stack push process. When all the statements in the method are executed, the method will be popped from the stack (if the stack is not popped, a stack will be generated at some point Overflow exception)

2. Sample code

public class Practice {
    public static void main(String[] args) {
        System.out.println("main 开始");
        show1();
        System.out.println("main 结束");
    }
    public static void show1(){
        System.out.println("show1 开始");
        show2();
        System.out.println("show1 结束");
    }
    public static void show2(){
        System.out.println("show2 开始");
        System.out.println("show2 结束");
    }
}
Copy after login

3. Process diagram of method calling of the entire program

3.1 JVM will automatically call the main method, Therefore, the main method first pushes the stack, and then executes the statements in the main method in sequence

What is the execution process of java method calls in memory?

##3.2 When encountering the show1() method, show1( ) method is pushed onto the stack, and executes the statements in the show1 method

What is the execution process of java method calls in memory?

3.3 When executing the statements in the show1 method, when show2() is executed , the show1 method pauses, then pushes the show2 method onto the stack, and then executes the content of the show2 method

What is the execution process of java method calls in memory?

3.4 When the content of the show2 method is executed, The show2 method will be popped off the stack and returned to the location where the show2 method was called in the show1 method. If there is no assignment operation on the return value, the following statements will be executed

What is the execution process of java method calls in memory?

3.5 After executing the show1 method, the show1 method will be popped from the stack, return to the location where the show1 method is called, and then execute the show1 method call and the subsequent statements

What is the execution process of java method calls in memory?4. Sample code running screenshot

The above is the detailed content of What is the execution process of java method calls in memory?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template