Home>Article>Java> Example analysis of the operating principle of the stack in the Java virtual machine

Example analysis of the operating principle of the stack in the Java virtual machine

WBOY
WBOY forward
2023-04-18 10:46:02 968browse

Operation Principle

1. Stack frames contained in different threads are not allowed to reference each other.

2. If the current method calls other methods, when the method returns, the current stack frame will return the execution result of this method to the current stack needle, and the virtual machine will discard the current stack frame, leaving the previous stack The frame becomes the current stack frame again.

3. Java methods have two ways of returning functions.

One is a normal function return, using the return instruction; the other is to throw an exception. No matter which method is used, the stack frame will be popped.

Example

public class StackFrameTest { public static void main(String[] args) { StackFrameTest stackFrameTest = new StackFrameTest(); stackFrameTest.method1(); } public void method1(){ System.out.println("method1()开始执行"); method2(); System.out.println("method1()执行结束"); } public int method2(){ System.out.println("method2()开始执行"); int i = 100; int m = (int)method3(); System.out.println("method2()即将结束"); return i + m; } public double method3(){ System.out.println("method3()开始执行"); double j = 3.1; System.out.println("method3()即将结束"); return j; } }

The above is the detailed content of Example analysis of the operating principle of the stack in the Java virtual machine. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete