Home > Java > javaTutorial > body text

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

WBOY
Release: 2023-04-18 10:46:02
forward
1018 people have browsed it

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;
    }
}
Copy after login

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!

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