How to End a Java Program

  1. System.exit() to Terminate a Java Program
  2. return to Exit From a Function in Java

This article shows all the best ways that we can use to end a running program in Java. We will go through a few examples to understand the situation and the easiest way to handle it.

System.exit() to Terminate a Java Program

To end a Java program, we can use the exit() method of the System class. It is the most popular way to end a program in Java. System.exit() terminates the Java Virtual Machine(JVM) that exits the current program that we are running.

Below is a simple example that uses the System.exit() method. Notice that it takes an integer, which is the status code. We pass 0 to the exit() function, which indicates that the termination happens successfully without any error. A non-zero status like 1 and -1 tells the compiler to terminate the program with some error or message.

You can see that the output only prints Statement 1 in the below example because the program exits after its execution and Statement 2 was never able to be executed.

public class Main {
    public static void main(String[] args) {
        System.out.println("Statement 1");
        System.exit(0);
        System.out.println("Statement 2");
    }
}

Output:

Statement 1

Process finished with exit code 0

return to Exit From a Function in Java

Sometimes we might have to terminate a function instead of the Java Virtual Machine, and that is when the return keyword of Java comes into play. This keyword is very useful when we are using functions in our program.

In formal language, return can be used to complete the method execution. It means that we can use this way to exit our function.

The following example shows how we can return a String from the function. If the value is false, it will return Statement 2 and then ignore Statement 3 as the execution of this method has been completed.


public class Main {
    public static void main(String[] args) {
       boolean value = false;
      
       String getValueFromFunc = func(value);
        System.out.println(getValueFromFunc);
    }

    public static String func(boolean value){
        if(value){
            System.out.println("Statement 1");
        }else{
            return "Statement 2";
        }
        return "Statement 3";
    }

}

Output:

Statement 2

But what if the value is true? Let’s find out in the next example.

The output prints Statement 1 as well as Statement3, which was ignored in the previous example. It happened because the statement block of if condition has println instead of a return.

public class Main {
    public static void main(String[] args) {
       boolean value = true;
      
       String getValueFromFunc = func(value);
        System.out.println(getValueFromFunc);
    }

    public static String func(boolean value){
        if(value){
            System.out.println("Statement 1");
        }else{
            return "Statement 2";
        }
        return "Statement 3";
    }

}

Output:

Statement 1
Statement 3