| by Arround The Web | No comments

How to End a Java Program

Java is one of the most popular programming languages. It can run any program on JVM (Java Virtual Machine). The program will terminate as JVM stops. Usually, Java programs terminate when they reach the end of the program; however, there are some situations where you may need to terminate a program during the execution of specific conditions. In such scenarios, end that particular program with the help of methods supported by Java.

This blog will teach you how to end a program in Java. So, keep reading!

How to End a Java Program?

In Java, you can end a program using:

    • System.exit() method
    • return statement

Check out each of the mentioned methods one by one.

How to End a Java Program Using System.exit() Method?

You can end a Java program by using the “exit()” method of the Java “System” class. It terminates the currently running JVM.

Syntax

To invoke the mentioned method, follow the given syntax:

System.exit(0)

 
Here, the System.exit() method has a parameter 0, which indicates that the program will terminate without any error.

Example 1: Printing Strings Before and After Ending a Java Program

In this example, we will terminate our program after printing one statement. In the main() method, we will first print a String “Java Programming” by using the System.out.println() method:

System.out.println("Java Programming");

 
Then, we will call the exit() method of the System class to terminate our program:

System.exit(0);

 
Lastly, we will try to print another String:

System.out.println("Java Programming language");

 

As you can see, the “Java Programming language” string is not displayed because the JVM is terminated before the execution of this line:

Example 2: Ending a Java Program Based on Condition

We will now terminate our program based on the condition added in an “if” statement, within the “for” loop. Firstly, we will create an integer array of even numbers:

int array1[]= {2,4,6,8,10,12,14,16,18,20};

 
We will print out the array values till the array element is greater than or equal to 10. If the added condition is evaluated as “true”, the program will print the specified message and terminate:

for (int i = 0; i < array1.length; i++) {
         if (array1[i] >= 10){
                System.out.println("Program terminated due to exit() method");
                System.exit(0);
            }
            else
                System.out.println(array1[i]);
      }

 
After executing the “for” loop, the message “Exit…” in the main() method will not print on the console because the program exits before the execution of this line:

System.out.println("Exit...");

 

The output shows the program was terminated when the value of the array was greater than or equal to 10:


Let’s head towards the other method!

How to End a Java Program Using return Statement?

The “return” statement is mostly used to return a value to a function; otherwise, the void return statement will end the execution of the current method or function.

Syntax

Add the keyword “return” to the point where you want to program executed to be stopped:

return

 
Now, we can check the examples with return statements to terminate a Java program.

Example 1: Ending main() Method Using return Statement

The “return” statement can terminate a Java program if it is the last statement executed within the main() method. If you add code after a return statement, the Java program may crash and throw an “Unreachable code” Exception because after running the return statement, no other line will be executed:

public static void main(String[] args) {
    System.out.println("Java Programming");
    return;
    System.out.println("Java Programming language");
}

 
Output

Example 2: Adding return Statement in “if” Condition

In this example, we will terminate the Java program with the “return” statement added in the “if” condition. It will act the same as the System.exit() method.

Firstly, we will create an integer array of even numbers named “array1”:

int array1[]= {2,4,6,8,10,12,14,16,18,20};

 
Next, we will print the values of array and terminate the program when an elements gets greater than or equals to 15:

for (int i = 0; i < array1.length; i++) {
    if (array1[i] >= 15){
            System.out.println("Program Exit due to return statement");
            return;
        }
       else
            System.out.println(array1[i]);
    }

 
The statement added in the main() method will not be executed because the Java program will end before that:

System.out.println("Exit...");

 

Output

We have provided all the necessary instructions for ending a Java program.

Conclusion

To end or terminate a program in Java, use the System.exit() method or a return statement. Both ways are helpful, but the most commonly used approach is the System.exit() method. The System.exit() method terminates the currently executing JVM, whereas the return statement is used to return values or stop the function execution. This blog discussed how to end or terminate a Java program.

Share Button

Source: linuxhint.com

Leave a Reply