• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return statement in main()

 
William Quantrill
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Daniela Ch
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Quantrill
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic