What is the correct format for the System.exit() method?

261 views Asked by At

I believe I'm not formatting the System.exit() method properly. This is how its formatted in my program:

  else if (input==6){
     System.exit();
  }

Is this correct syntax?

2

There are 2 answers

0
dave On BEST ANSWER

System.exit(int) takes an integer argument, but otherwise you can call it to terminate your program.

Why do you want to call your main() method again? It is normally the static entry point to your program. If there's some code you wish to repeat, I'd suggest putting this in its own method and calling that (from main() and your code in the question).

0
arjuns On

System.exit(int status) takes an int parameter. The status is not especially important; it's more of a feedback for the System if the exit went well. It's not important in Java; it's just something inherited by Java from older programming languages.

Just for your reference:

0 = fine
anything else = represent different codes

I would suggest using System.exit(0);