Open In App

Run-Time Stack Mechanism in Java [Use the updated images]

Last Updated : 22 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 an 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.

Let’s have a look at the below program to understand the working of the run-time stack.

Case 1: Normally (graceful termination)

Construction of run-time Stack : 

  1. Firstly, the main thread will call the main() method, and the corresponding entry will be in the stack.
  2. After that main() method is called the 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 the stack.
  4. Finally, moreFun() is not calling any method and it will print Hello Geeks!

Java




// 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!");
    }
}


Output

Hello Geeks!

Destruction of the run-time stack: After printing Hello Geeks!, its corresponding entry will be removed from the stack and it will go to the 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 has 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 objects because exceptions 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 the main thread. If the main thread(main method) doesn’t have any exception handling code the JVM also terminates the main method abnormally and the default exception handler is responsible to print the exception message to the output screen which is the part of JVM.

Java




// 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");
    }
}


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)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads