Why is return needed even after System.exit(0);

Because as far as the compiler is concerned, System.exit() is just another method call.

The fact that what it does is end the process can only be found out from the implementation (which is native code, not that it makes any difference).

If you have to put System.exit() in your code (usually it’s best to avoid it, unless you want to return a code other than 0), it should really be in a method that returns void, main() for example. It’s nicer that way.

As for the reachability, the explanation is the same: return is a keyword of the Java language, so the compiler or the parser the IDE uses can tell that it’s theoretically impossible for code after the return statement to be executed. These rules are defined here.

Leave a Comment