Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return() and System.exit()

 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the diff between return() and System.exit() in main()?
Thanks,
Jenny
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Corey
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, Corey! Your answers are always so clear!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic