aspose file tools*
The moose likes Beginning Java and the fly likes Quick question regarding System.exit() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quick question regarding System.exit()" Watch "Quick question regarding System.exit()" New topic
Author

Quick question regarding System.exit()

Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
Relearning java from the beginning (tedious yes I know) and I've run into something that looks right but I'm not positive. Here is the code for a basic determine if the integer is even or odd:
firstNumber= JOptionPane.showInputDialog("Enter integer:");

number1 = Integer.parseInt(firstNumber);
remainder = number1 % 2;
if(remainder == 0)
{
JOptionPane.showMessageDialog(null, "The number is even", "Even or Odd", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}

JOptionPane.showMessageDialog(null, "The number is odd", "Even or Odd", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);

My question is, and I am leaning toward that its okay, but regarding System.exit(0), won't the program exit as soon as that line is run? If so, is this bad programing style to have it inside an if statement?
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
Normally, invoking a GUI starts up some additional threads which keep running after the methods that call them are finished. When running a standalone application, you call System.exit() to shut down the whole JVM, which, of course, stops those threads. However, when running an applet, you don't call System.exit because the JVM is used for stuff other than your program.
As for style, this might be a place to use an else cause on your if statement. (I'll stop there.)
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
And the flaws of my book begin to show themselves. The JOptionPanes were part of an application. I probably would have used an else but for some reason I'm restraining myself to only use the symantics that have been intoduced as I move through the book.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick question regarding System.exit()