• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What does System.exit(0); do?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yes, it will close your application completely.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I presume you have read the System class documentation?
 
reply
    Bookmark Topic Watch Topic
  • New Topic