Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Where would the return from main go to?

 
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?
 
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).
 
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 ]
 
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 ]
 
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 ]
 
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
 
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.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic