Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quick question regarding System.exit()

 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic