Run-time Stack mechanism in Java

Prerequisite : Exceptions in Java

For every thread JVM (Java virtual machine) creates a run-time stack.

  • Each and every call performed in a thread is stored in the stack.
  • Each entry in the run-time stack is known as activation record or stack frame.
  • After completing every method call by the thread is removed from the corresponding entry of the stack.
  • After completing all the methods, the stack will be empty and that run-time stack will be destroyed by the JVM before terminating the thread.

Lets have a look on the below program to understand the working of run-time stack.



CASE 1 : Normally (graceful termination)

  • Construction of run-time Stack :
    1. Firstly, the main thread will call main() method and the corresponding entry will be in the stack.
    2. After that main() method is calling fun() method, which will store in the stack.
    3. In the fun() method, moreFun() method is called. Therefore at last moreFun() will be stored in stack.
    4. Finally, moreFun() is not calling any method and it will print Hello Geeks!

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate run time
    // Run-time stack mechanism in
    // normal flow of Exception handling
    class Geeks {
        public static void main(String[] args)
        {
            fun();
        }
        public static void fun()
        {
            moreFun();
        }
        public static void moreFun()
        {
            System.out.println("Hello Geeks!");
        }
    }

    chevron_right

    
    

    Output:

    Hello Geeks!
    

  • Destruction of run-time stack : After printing Hello Geeks!, its corresponding entry will be removed from the stack and it will go to fun() method and there is nothing for execution that’s why the entry of fun() method is removed from the stack and so on.When the stack is empty then the run-time stack is destroyed by the JVM.

CASE 2 : Abnormally (abnormal termination)

  • Construction of run-time Stack :
    1. The example below have ArithmeticException at method moreFun() location, the JVM will check any exception handling code is there. If not, then method moreFun() will be responsible to create exception object because of exception are raised on method moreFun() and corresponding entry from the stack will be removed and the control goes to method fun().
    2. JVM again go to the caller method to check if it is having any exception handling code are not. If not JVM terminates the method abnormally and deleted the corresponding entry from the stack.
    3. The above process continues until main thread. If the main thread(main method) does’t have any exception handling code the JVM also terminates the main method abnormally and default exception handler is responsible to print the exception message to the output screen which is the part of JVM.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate run time
    // Run-time stack mechanism in
    // normal flow of Exception handling
    public class ExceptionHandling {
        public static void main(String[] args)
        {
            fun();
        }
        public static void fun()
        {
            moreFun();
            System.out.println("Method fun");
        }
        public static void moreFun()
        {
            System.out.println(10 / 0);
            System.out.println("Method moreFun");
        }
    }

    chevron_right

    
    

    Runtime error:

    Exception in thread "main" java.lang.ArithmeticException: / by zero
        at ExceptionHandling.moreFun(ExceptionHandling.java:16)
        at ExceptionHandling.fun(ExceptionHandling.java:11)
        at ExceptionHandling.main(ExceptionHandling.java:7)

  • Destruction of run-time stack

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.