Java System.exit() Method

Author: Tatyana Milkina

The System.exit() method in Java is used to terminate the currently running Java program. It takes an argument of type int, which represents the exit status.

Typically, passing 0 means the program terminated normally without any errors. Any non-zero value usually indicates an abnormal termination due to an error or unexpected condition.

Here is a simple example:

public class SysExitExample {
    public static void main(String[] args) {
        System.out.println("Before exit.");
        method(true);
        System.out.println("This line will not be executed.");
    }

    public static void method(boolean flag) {
        if (flag) {
            System.exit(0);
        }
        System.out.println("This line inside method will not be executed.");
    }
}

Output of the program:

Before exit.
Курс 'Java для начинающих' на Udemy Курс 'Java для начинающих' на Udemy
Read also:
Comments