The Java programming language is powerful. If you are a programmer or a developer, then Java is one of those compulsory bits of knowledge that should have existed somewhere inside you. Honestly, there is a lot of Java knowledge you need to know. So, this article is actually all about how one can end a program in Java and how it is done.
System.exit() Ends a Java Program
For those of you who do not know how to end one’s program in Java, it is actually possible through a number of ways. First of all, we can use the exit() method of the System class; honestly, it is the most popular way in Java to stop a program. Thus, calling System.exit() terminates the Java Virtual Machine(JVM) that exits the currently running program.
To really understand it, below is a simple example about how to end a program in Java that uses the System.exit() method. However, take notice that it takes an integer, which is the status code. Then, we pass 0 to the exit() function, which indicates that termination occurs without error. A non-zero status such as 1 and -1 instructs the compiler to terminate the program with some error or message.
How to terminate a java program by using the return statement?
It is another way for Java to stop running the program; return is used for it.
Also, the term return signifies an event that comes to halt execution of this prevailing method and passes control to the invoking one. It is already a reserved word most familiar to the compiler; also, return can terminate a java program if it is in the last point inside the main method of the class being executed. Therefore, if you put code after a return statement, the compiler spits out an error saying unreachable code.
Output:
Statement 1
Process ended with exit code 0
In Java, one approach to stop a program is the following: If the given condition in the if statement matches, the program will be terminated.
Below is an example to illustrate this.
Output:
10
20
30
Terminate JVM
So, as highlighted, the Java program ceases functioning the moment the actor’s property becomes greater than or equals to 40 when being iterated upon. It neither continues the loop nor prints Loop ends here after calling System.exit() method.
How to end java program with return
But we have one more way in which we can stop a Java program. The return statement can be used to end the program.
The return statement terminates the execution of the currently running method and control goes back to the calling method. Now it also is reserved keyword already known to the compiler. Further, it can also end the Java program if it happens to be the last statement within the main method of the class being executed. So if you put some code after a return statement, the compiler would throw compilation 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.
Output:
- Prime: true
is 27 Prime: false
is 79 Prime: true
Sum up
And that’s all we have to finish a Java program. Hope it helps you.

Read Dive is a leading technology blog focusing on different domains like Blockchain, AI, Chatbot, Fintech, Health Tech, Software Development and Testing. For guest blogging, please feel free to contact at readdive@gmail.com.