This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What does System.exit(0); do?

 
Waria Ahmed
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got a basic menu program at the moment, and I am compiling and running it in command prompt.

Problem is, i have declared the code to exit system when 2 is pressed. but all it does it quit the program and return to command prompt. Is this right? or is it meant to close cmd as well?
 
Waria Ahmed
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically my quesiton is:

If i created an executable version of the program at the end, and i pressed 2 to quit the program, would it exit completely?
 
Norm Radder
Rancher
Posts: 1209
13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it meant to close cmd as well?

No. The console window should stay open. The exit is for the JVM, not the JVM's calling program.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Waria Ahmed:
basically my quesiton is:

If i created an executable version of the program at the end, and i pressed 2 to quit the program, would it exit completely?


Most of the time, yes this would exit the application completely if the executable is double clicked on.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is all right to use System.exit(int) in a simple application with a single thread, but in a multi-threaded application it can be harmful; if you stop your JVM while a thread is still doing something, that something might only be half-done. You might have half a record written to a file, and that file will be corrupt for next time it is used.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes, it will close your application completely.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have read the System class documentation?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic