System.exit() Java Method
java.lang.System.exit() methods stops running or terminates Java program by shutting down Java virtual machine.
This method takes a status code.There are different state where a negative value means abnormal program termination.
exit(0)-is successful termination.
exit(-1) or any other negative value shows unsuccessful termination.This method is void and has no return type.
Syntax:
Output:
This method takes a status code.There are different state where a negative value means abnormal program termination.
exit(0)-is successful termination.
exit(-1) or any other negative value shows unsuccessful termination.This method is void and has no return type.
Syntax:
public void exit(int status)[
import java.util.*; import java.lang.*; public class ExitMethodExample { public static void main(String[] args) { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; for (int i = 0; i < arr.length; i++) { if (arr[i] >= 5) { System.out.println("exit..."); // Terminate JVM System.exit(0); } else System.out.println("arr["+i+"] = " + arr[i]); } System.out.println("End of Program"); } }]
Output:
run:arr[0] = 1arr[1] = 2arr[2] = 3arr[3] = 4exit...BUILD SUCCESSFUL (total time: 3 seconds)