Java stop program: How to end the program in Java

The Java programming language is a powerful programming language. In case you are a programmer or a developer, Java is one of the important and must-have knowledge you have. Honestly, there is a lot of java knowledge you need to know. So, following the series of articles about Java is the article about Java stop program and how to end it.

Let’s discover it with ArrowHiTech through this article below ! 

System.exit() to Terminate a Java Program

To stop a Java program, there is a lot of ways to do this. Firstly, we can use the exit() method of the System class, honestly, it is the most popular way to stop a program in Java. System.exit() terminates the Java Virtual Machine(JVM) that exits the current program that we are running.

To fully understand it, below is a simple example about stop a Java program that uses the System.exit() method. However, notice that it takes an integer, which is the status code. Then, 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.

Moreover, you can see that the output only prints Statement 1 in the below example. This is the program exits after its execution and Statement 2 was never able to be executed.

java stop program example

Output:

Statement 1

Process finished with exit code 0

How to end program in an if statement in java

One of the others way about Java stop program based on if statement condition. Then, please follow example below.

exit program based on If statement

Output:

10
20
30
Terminate the JVM

So, as you can see, the Java program stopped as soon as array element was greater or equals to 40 while iterating. It neither continued the loop nor printed Loop ends here after calling System.exit() method.

Using return to end current method

We mention one more way to exit from current method, you can use return statement.

Retuern statement stops the execution of current method and return it to calling method. Then, it is a reserved keyword which compiler already knows. Moreover, return can end java program if it is the last statement inside main method of class being executed. Therefore, if you put code after a return statement, compiler will give error about unreachable code.

Example 1

public class JavaHelloWorld {

public static void main(String[] args) {

System.out.println(“Before calling Syste.exit()”);

return;    

    System.out.println(“After calling Syste.exit()”);   

All in all, preceding the java program will give you a compilation error about unreachable code at line number 6.

Example 2

Let’s say you want to write the isPrime method to check if the number is prime or not. Of course, you can call isPrime() method from the main method and return true or false based on whether the number is prime or not.

isPrime

Output:

17 Prime: true
is 27 Prime: false
is 79 Prime: true

Sum up

All in all, that’s all about how to end program in java. Moreover, we hope this is a useful post for you. However, if you still have questions about Java or Java stop program, please leave a comment below. Besides that, in case you want to know more about Java or other programming language, you can visit ArrowHitech website. With many years of experience in web development, we believe we can make you satisfied

Tags

Share