wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes return statement in main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return statement in main()" Watch "return statement in main()" New topic
Author

return statement in main()

William Quantrill
Ranch Hand

Joined: Dec 23, 2002
Posts: 36
In the code below (from Wrox press beginning Java2), what is the purpose of having a return statement in the main method vs not having a return statement. Just seems odd since we dont return anything.

(Marilyn added code tags)
[ December 25, 2002: Message edited by: Marilyn de Queiroz ]

"The Rebel army is now the legitimate property of the Army of the Potomac." - Joseph Hooker spoke these pompous words shortly before he was soundly defeated by Robert E. Lee at Chancellorsville
Daniela Ch
Ranch Hand

Joined: Dec 13, 2002
Posts: 96
Hi,
I know that a return statement in the main will end the program, since it returns control back to the system.
In this case, the return is at the end of the main routine...It is a way to stop the threads, since the return will Interrupt their run().
I think...
/Daniela
[ December 25, 2002: Message edited by: Daniela Ch ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
The "main" thread will end when it reaches the ending curly brace whether "return" is there or not. It will also stop the program because the new threads are daemon threads, but you'll notice that some stuff may still print after main() has ended.

I see no function for "return" in this context.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
William Quantrill
Ranch Hand

Joined: Dec 23, 2002
Posts: 36
Originally posted by Marilyn de Queiroz:
The "main" thread will end when it reaches the ending curly brace whether "return" is there or not. It will also stop the program because the new threads are daemon threads, but you'll notice that some stuff may still print after main() has ended.

I see no function for "return" in this context.

Thats kind what im thinking as well...
it doesnt hurt either though
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Here's a nice little example showing that 'return' in main can be useful, i.e. it doesn't necessariliy terminate the program, but it does terminate the current invokation of function main itself:

kind regards
 
jQuery in Action, 2nd edition
 
subject: return statement in main()
 
Similar Threads
some problems regd threads as in velmurugan's notes
Cancelling Timer and TimerTasks
Need practical example of wait() and notify()
about threads, using locks of the objects in synchronized blocks
status of deamon threads