File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return() and System.exit() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return() and System.exit()" Watch "return() and System.exit()" New topic

return() and System.exit()

Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
what is the diff between return() and System.exit() in main()?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Try this example:

If you run this program as is, the application will terminate immediately. However, if you comment out line 2 and uncomment line 1, you'll see that the application gets caught in an infinite loop.
If you take a look at the API Spec for System, you can see the description of the exit method states:

Terminates the currently running Java Virtual Machine...

This means that your application will terminate, no matter what is happening. A return, however, signifies, the successful completion of a method.
I hope that helps,

SCJP Tipline, etc.
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Thanks a lot, Corey! Your answers are always so clear!
I agree. Here's the link:
subject: return() and System.exit()
It's not a secret anymore!