This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Where would the return from main go to? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Where would the return from main go to?" Watch "Where would the return from main go to?" New topic
Author

Where would the return from main go to?

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
HI All,

When we write the return statement in the main method where would the control go to?


Thanks,
Shekar
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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).


42
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
(...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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Oneal Shaha
Ranch Hand

Joined: Aug 11, 2004
Posts: 98
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 ]

-Oneal
Mattias Arthursson
Ranch Hand

Joined: Jul 26, 2004
Posts: 90
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 ]

Admit nothing. Blame everyone. Be bitter.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Jul 26, 2004
Posts: 90
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

Joined: Aug 11, 2004
Posts: 98
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

Joined: Mar 13, 2004
Posts: 1272
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.


Mike Gershman
SCJP 1.4, SCWCD in process
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Where would the return from main go to?