CodeGym /Java Blog /Core Java /System.exit() in Java
Author
Pavlo Plynko
Java Developer at CodeGym

System.exit() in Java

Published in the Core Java group
The System class in Java contains fields and methods to handle the SystemOne of them is the System.exit () method used in the Java language when you need to terminate a program or rather JVM instance that is currently running. Any line inserted below the exit method will be unreachable and will not be executed.

Syntax of System.exit() method

The syntax of the System.exit() method is as follows.

public void static(int status)
So you can see that it’s a static method. Actually, all the methods in System class are static methods. The exit() method takes an integer as an argument and returns nothing. So you will call the exit method as System.exit(i) where i is an integer. This integer is called “exit status” and can be a zero or non-zero element. If the status is zero — exit(0), the program will have a successful termination. A non-zero status — exit(1) indicates abnormal termination of the JVM.System.exit() in Java - 1

Example of System.exit() method

Let’s see two simple examples of the exit() method with status as zero and non zero integers. In our first example, there is a loop over an array of colors. When the loop meets “green”, the application needs to be terminated.

import java.lang.*;  
 
class Main {
  public static void main(String[] args) {
    String colors[]= {"red","blue","green","black","orange"};  
    for(int i=0;i<colors.length;i++)  {  
      System.out.println("Color is "+colors[i]);
      if(colors[i].equals("green")) {  
        System.out.println("JVM will be terminated after this line"); 
        System.exit(0); 
      }
    }  
  }
}
The following output will be displayed.System.exit() in Java - 2The Terminal didn’t show any exit code in the output because we used zero as the status. As zero denotes successful termination, there is no need to print an exit code. So let’s use a positive integer as the status in our next example. In this example, we create a loop that generates random numbers between 0 and 10. If the generated number is 2,3, or 7, the Application needs to be terminated, and it should print which number causes the termination. See the code below.

import java.lang.*;  
import java.util.Random; 
 
class Main {
  public static void main(String[] args) {
    System.out.println("program will be terminated when values are 2, 3, or 7");  
    int i;
    Random number=new Random();
    while(true){
      i = number.nextInt(11);
      System.out.println("Random Number is "+i); 
      if(i==2||i==3||i==7){
        System.out.println("Value is "+ i + " your program will be terminated now");  
        System.exit(i);
      }
    }
  }
} 
When I executed the code, I got the following output.System.exit() in Java - 3As you can see, number 3 caused the abnormal termination of the application. Now, let’s see how the status code can be used effectively.

How to use the status code effectively

Status code is usually important when you run the Java program through a command-line interface (CLI). Usage of status code is beneficial if your intent is to use this program to communicate with other standard tools, programs, or operating systems. If the program is intended to communicate with an operating system, you can use status codes specific to the operating system. For example, 128 is the standard status code in UNIX to describe “Invalid argument to exit”.

When to use the System.exit method

One of the typical usages of System.exit is when an abnormal condition is exhibited in the program, and you need to terminate the program immediately without causing further problems. Another use is when there is a requirement to terminate the program other than from the main method. There is a special construct called “Shut Down Hooks” in Java, which allows developers to plug in a code snippet that should be executed before the termination of JVM. They are particularly useful to do clean up operations. The System.exit method is used to call shutdown hooks in such cases.

Conclusion

In this article, we learned the System.exit() method in detail. The System.exit means in Java is a way to terminate the JVM. The System.exit method doesn’t return anything because the application will not execute any code below the exit() method. We also discuss the real-world uses of the exit() method. However, the Java community discourages the use of exit method as long as you have other options.
Comments (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Kamran Muessig Level 1, Canada
12 October 2022
For me, the exit status code doesn't show up in the eclipse terminal. How can I change it so that it does show up?