Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Where would the return from main go to?

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI All,

When we write the return statement in the main method where would the control go to?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
returning from main will cause the application to shut down and the JVM to terminate (unless there's other threads running in which case the application will then terminate as soon as those other threads complete).
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(...as long as those other threads aren't daemon threads.)

Of course, if the main method had merely been invoked from another method, then control would return to the invoking method.
[ August 25, 2004: Message edited by: Dirk Schreckmann ]
 
Oneal Shaha
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So after the return statement in 'normal' main method, no object is Garbage collected as the garbage collection thread is a daemon thread.

And neighter the finally block is executed after the return statement in main()(is it right ???)

So return in main is the last ever statement executed...
[ August 30, 2004: Message edited by: Oneal Shaha ]
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oneal Shaha:
And neighter the finally block is executed after the return statement in main()(is it right ???)
[ August 30, 2004: Message edited by: Oneal Shaha ]


The correct question would not be what happens after the return statement of the main() method, but rather what happens when control moves out of the main() method. A finally block will always be executed before control moves out of the method, just as for any other method.
[ August 30, 2004: Message edited by: Mattias Arthursson ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could we right the return statement in main method, cuz it is marked as void? would compiler allow us to do this?
 
Mattias Arthursson
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by adeel ansari:
could we right the return statement in main method, cuz it is marked as void? would compiler allow us to do this?


As long as you don't specify a return value it is perfectly legal to include a return statement in a void method, i.e.

 
Oneal Shaha
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mattias,
You are absolutly right

Adeel,
Though we mark a method as void we can always write the simple return statement of syntax " return; ".
Also in constructores though void return type also is not allowed. But still we can write the simple return statements in constructores
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So after the return statement in 'normal' main method, no object is Garbage collected as the garbage collection thread is a daemon thread.

After main() ends and the jvm terminates, the virtual address space it was running in is either liquidated or made available to the next command/program launched in that address space.

Garbage collection is a fine instrument within the jre that frees up the memory occupied by individual objects during program execution. The operating system's process termination logic just eliminates from memory the jvm and everything that the jvm created as if they never existed in memory.

Also, the jvm shares space in real (hardware) memory and swap/paging disk files. This space is marked available to other processes in the system.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic