• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Working with JoptionPane

 
Ranch Hand
Posts: 71
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I going through a old book on Java(copyright 2005) and I noticed that in the driver class I have to write the line system System.exit(0); I was wondering why do we do this. The program runs okay just have a question about the System.exit(0) code.

Here is my code






Thank you for taking time to help me out
 
Bill foster
Ranch Hand
Posts: 71
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry here is my code in the tags:







 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What have you found after you googled it?
Some results
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please remind yourself how to use code tags; I have corrected them for you and that is why the post looks all right now I also removed some unnecessary blank lines.

I am not sure the search is going to be that helpful; it seems to be about the difference between System.exit(0) and System.exit(somethingElse). Please tell us which book that came from. I think you are right; System.exit is unnecessary there. In some places (e.g. when closing a GUI which uses code reading from a file), System.exit can be positively harmful.
 
Liutauras Vilda
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/344527/GUI/java/System-exit-JOptionPane

8 years old thread. Campbell Ritchie is participator in it also. Refresh your minds
Thant might would answer to some doubts.
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you get to my age you will have difficulty remembering what you did eight minutes ago, never mind eight years

Maybe the two threads are discussing the same book. It would be very interesting to know which book it is.
 
Bill foster
Ranch Hand
Posts: 71
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book is "Java Concepts" By Cay Horstmann. This is the first book I learn Java from. I think it was when Java 5.0 came out.
 
Bill foster
Ranch Hand
Posts: 71
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the reason why we use the System.exit(0); is because JOptionpane is not thread safe. I actually looked at the API and found this:JOptionPane API


Swing's thread policy: Thread policy
 
Campbell Ritchie
Marshal
Posts: 80230
424
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, 2005 was in the earlier days of Java5.

I don't have that book, but I do have a 2005 edition of Core JavaII by Horstmann and Cornell. And this is what it says

Horstmann and Cornell, page 60 wrote:NOTE: If you do not have JDK5.0 or above, you will have to work harder to read user input. The simplest method is to use an input dialog (see figure 3-6).
String input = JOptionpane.showInputDialog(promptString)
The return value is the strin g that the user typed.
For example, this is how you can query the name of the user of your program:
String name = JOptionPane.showInputDialog("What is your name?");
Reading numbers requires an additional step. The JOptionPane.showInputDialog method returns a string, not a number. You use the Integer.parseInt or Double.parseDouble method to convert the string to its numeric value. For example,
String input = JOptionPane.showInputDialog("How old are you?");
int age = Integer.parseInt(input);
If the user types 45, then the string variable input is set to the string "45". The Integer.parseInt method converts the string to its numeric value, the number 45.
The JOptionPane class is defined in the javax.swing package, so you need to add the statement
import javax.swing.*;
Finally, whenever your program calls JOptionPane,showInputDialog, you need to end it with a call to System.exit(0). The reason is a bit technical. Showing a dialog box starts a new thread of control. When the main mathod exits, the new thread does not autoatically terminate. To end all threads, you call the System.exit method. (For more information on threads, see Chapter 1 of Volume 2.) The following program is the equivalent of Example 3-2 prior to JDK5.0.

Before Java5 it was common to use option panes for keyboard input because buffered readers are so awkward to use. The idea in that particular example is that opening the dialogue window starts the event dispatch thread (??) (=EDT), and terminating the main method terminates the main thread. That may leave the EDT running, so you need System.exit to terminate all threads.
  • 1: This is something you had to do in JDK1.4 and it is no longer necessary because you usually use Scanner for keyboard input.
  • 2: This uses JOptionPane in a non‑GUI context.
  • If you call System.exit from a dialogue window in a Swing app, you will close the entire app.

    The fact that Swing is not thread‑safe is not relevant to this particular problem. It is because in a non‑GUI context you start another thread. On my current box using Java8 it is not possible to discern the different threads of execution. You can see the opposite effect whereby you shut down the main thread like this:-That particular version only works in Java8 and it requires a command line argument:-
    java WontStop "Catch Me If You Can"
     
    Liutauras Vilda
    Sheriff
    Posts: 8988
    652
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:1: This is something you had to do in JDK1.4 and it is no longer necessary because you usually use Scanner for keyboard input.


    Well, thank you for information Campbell Ritchie.
    But still, what is your opinion, if Java 1.8 is used, and lets assume JOptionPane for input still in use, does it still need to end up with System.exit?
    Actually in some cases I still find useful to use JOptionPane for input, but never used System.exit after that.
     
    Campbell Ritchie
    Marshal
    Posts: 80230
    424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Don't know. I tried something very similar to Horstmann's class using Java8 with and without the exit call. In both cases the JVM exited, though maybe quicker with the exit call.

    I am very suspicious about System.exit myself, however.
     
    Bill foster
    Ranch Hand
    Posts: 71
    2
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the info I think it does not make a difference, I running JGrasp as my compiler and found no issues with or without System.exit(0). I notice that one of the methods have a -> is this a lambda expression in the won't stop class.
     
    Campbell Ritchie
    Marshal
    Posts: 80230
    424
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, it is a λ; that is why I said that code only works in Java8.

    And … you're welcome
    reply
      Bookmark Topic Watch Topic
    • New Topic