print
Advertisment
Advertisment

Runtime stack mechanism

For every thread, JVM will create a separate stack at the time of Thread creation.
  • All method calls performed by that thread will be stored in that stack.
  • Each entry in the stack is called "Activation record" (or) "stack frame".
  • After completing every method call JVM removes the corresponding entry from the stack.
  • After completing all method calls JVM destroys the empty stack and terminates the program normally.

Normally termination of the program

Construction of run-time Stack :

  • Firstly, the main thread will call the main() method, and the corresponding entry will be in the stack.
  • After that main() method is called the fun() method, which will store in the stack.
  • In the fun() method, moreFun() method is called. Therefore at last moreFun() will be stored in the stack.
  • Finally, moreFun() is not calling any method and it will print Welcome to B2E Programmers
class B2eProgrammers {
    public static void main(String[] args)
    {
        method1();
    }
   
    public static void method1()
    {
        method2();
    }
   
    public static void method2()
    {
        System.out.println("Welcome to B2E Programmers");
    }
}

Output :

	
	
	Welcome to B2E Programmers
	

Note: After printing "Welcome to B2E Programmers", 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

Advertisment

abnormal termination of the program

  • 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 the corresponding entry from the stack will be removed and the control goes to method fun().
  • JVM again goes to the caller method to check if it is having any exception handling codes are not. If not JVM terminates the method abnormally and deleted the corresponding entry from the stack.
  • 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 the JVM.
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");
    }
}

Output :

	
	
	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)
	
Advertisment
Advertisment
arrow_upward