Ho In this post, we will see how to end program in java.
You can use
System.exit(0)
to end program in java.
Table of Contents
Using System.exit() to end java program
You can use exit()
method of System class to end java program.
System.exit()
causes Java virtual machine to exit and terminate the current process. You can call System.exit()
anywhere in the program, resulting in JVM termination.
As System.exit()
is the static method of System
class, the compiler does not know what it will do, hence does not give unreachable code error.
If we pass 0 to System.exit() method, it indicates that termination happened successfully and pass this value to operating system.
exit(0)
: It indicates successful end of java program.exit(-1) or exit(1) or exit(any non zero value)
: It indicates unsuccessful end of java program.
Example to demonstrate System.exit() method
Here is an example:
1 2 3 4 5 6 7 8 9 |
public class JavaHelloWorld { public static void main(String[] args) { System.out.println("Before calling System.exit()"); System.exit(0); System.out.println("After calling System.exit()"); } } |
Output:
As you can see, it did not print After calling Syste.exit()
after calling System.exit()
method.
How to end java program in an if statement in java
You can exit program in java based on if statement condition.
Here is an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog.published; public class SystemExitExampleIfStatement{ public static void main(String[] args) { int arr[] = {10, 20, 30, 40, 50, 60, 70, 80}; for (int i = 0; i < arr.length; i++) { if (arr[i] >= 40) { // end java program System.out.println("Terminate the JVM"); System.exit(0); // Terminates JVM } else { System.out.println(arr[i]); } } System.out.println("Loop ends here"); } } |
Output:
20
30
Terminate the JVM
As you can see, program ended 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
If you just want to exit from current method, you can use return statement.
return
statement stops the execution of current method and return it to calling method. It is a reserved keyword which compiler already knows.
return
can end java program if it is the last statement inside main method of class being executed. If you put code after a return statement, compiler will give error about unreachable code.
Here is an example:
1 2 3 4 5 6 7 8 9 |
public class JavaHelloWorld { public static void main(String[] args) { System.out.println("Before calling Syste.exit()"); return; System.out.println("After calling Syste.exit()"); } } |
Preceding java program will give you compilation error about unreachable code at line number 6.
Another example:
Let’s say you want to write isPrime method to check if number is prime or not. You can call isPrime()
method from main method and return true
or false
based on whether number is prime or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; public class isPrimeMain { public static void main(String[] args) { System.out.println("is 17 Prime: "+isPrime(17)); System.out.println("is 27 Prime: "+isPrime(27)); System.out.println("is 79 Prime: "+isPrime(79)); } public static boolean isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { return false; } } return true; } } |
Output:
is 27 Prime: false
is 79 Prime: true
That’s all about how to end program in java.