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 how to end a program in Java and how to end it.

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

System.exit() to Terminate a Java Program

For anyone who doesn’t know how to end program in Java, 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 how to end program in Java 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 Java 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.

How to end java program using return

We mention one more way for Java stop program, 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: How to end program in java

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 how to end program in Java, please contact us. ArrowHitech has become one of the superior IT outsourcing Vietnam company. We are experienced professionals in the field of  ECommerce, mobile apps development , CMS website development as well as Salesforce and Software Consultant & Development.

Tags

Share