public class WrapperTest {
static {
print(10);
}
static void print(int x) {
System.out.println(x);
System.exit(0);
}
}
In the above code System.exit(0)
is used to stop the program. What argument does that method take? Why do we gave it as 0
. Can anyone explain the concept?
exit(0) : Generally used to indicate successful termination. exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination. Note : This method does not return any value.
exit() it will do so with a non-zero status (as long as the main thread ends and there are no running daemon threads). That's all about Why you should not use System. exit() inside Java application. It can be dangerous and potentially shut down the whole server, which is hosting other critical Java application.
The typical use-case for System. exit is when there is an abnormal condition and we need to exit the program immediately. Also, if we have to terminate the program from a place other than the main method, System. exit is one way of achieving it.
System.exit() Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
From the JAVA Documentation:
The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
And Wikipedia adds additional information.
It's the return value that the Java process will report to the calling process.
It hasn't really got a precise definition, but the usual convention is that 0
means success and any non-zero value represents a failure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With