Operators · Lesson 7/8
88%
⏱ 10–15 min

Java System.exit() Method

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 Core

1. Java Introduction
2. Run Your First Java App
3. Java Syntax
4. Java Operations
5. Operators
6. Arrays
7. Sorting Algorithms
8. OOP Basics
9. Lambda Expressions
10. Stream API
11. Inner Classes and Exceptions
12. Git & GitHub
‹ Previous lesson Next lesson ›