Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of System.exit(0) [duplicate]

Tags:

java

exit-code

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?

like image 364
Warrior Avatar asked Jan 19 '09 11:01

Warrior


People also ask

Why do we use system Exit 0?

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.

Is it good to use system exit?

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.

Why do we use system exit?

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.

What does system Exit 0 do in Android?

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.


2 Answers

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.

like image 88
Xn0vv3r Avatar answered Oct 18 '22 05:10

Xn0vv3r


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.

like image 33
Joachim Sauer Avatar answered Oct 18 '22 04:10

Joachim Sauer