Java function Call stack

Source: Internet
Author: User
Tags throwable java se

Java's function call stack is the Java Virtual machine stack, which is thread-private and is created with threads to store stack frames.

The stack frame is created as the method is called and destroyed as the method ends. It can be said that the stack frame is the abstraction of a method.

You can then understand the function call process by printing out the stack frame information in the Java Virtual machine stack. The Java code used to implement this process is as follows:

Package Methodcall;public class Methods {public void method1 () {method2 ();} public void Method2 () {method3 ();} public void Method3 () {Throwable ex = new Throwable ();/** * Throwable getstacktrace () can return the virtual machine stack information of the current thread, returning the first element of the array is the top element of the stack, the most The latter element is the bottom element of the stack. */stacktraceelement[] stackelements = Ex.getstacktrace (); System.out.println (stackelements.length); for (Stacktraceelement stacktraceelement:stackelements) { System.out.println (Stacktraceelement.getmethodname ());}}}


Package Methodcall;public class Methodcall {public static void main (string[] args) {//TODO auto-generated method Stubmeth ODS methods = new methods (); Methods.method1 ();}}

The output results are as follows:

4
Method3
Method2
Method1
Main

Reference documents:

Java Virtual Machine specification Java SE 7

Java function Call stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.