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
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 astack 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 codepublic 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 结束"); } }
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
##3.2 When encountering the show1() method, show1( ) method is pushed onto the stack, and executes the statements in the show1 method3.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
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
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
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!