Win a copy of hapi.js in Action this week in the HTML, CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how void works in the sense of main() method

 
shivam upmanyu
Greenhorn
Posts: 2
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keyword void represents the return type. A method that returns no data returns
control to the caller silently
. In general, it’s good practice to use void for methods that
change an object’s state
. In that sense, the main() method changes the program state
from started to finished.....  
NOW WHAT DOES THAT MEANS RETURN CONTROL TO THE CALLER AND METHOD THAT CHANGE AN OBJECT'S STATE...
 
Junilu Lacar
Sheriff
Posts: 9338
96
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

When  a method is called, it "gains control" of what happens in the program. It keeps control until it explicitly returns via a return statement or implicitly returns after executing the last statement in it. When a method returns, execution continues from where it left off when the method was called. So, the method is said to "return" control to the code that called it.

The rest of the statement regarding main() and returning void is not a very good explanation. Please state your source. Provide a link to wherever you found this if you can.
 
shivam upmanyu
Greenhorn
Posts: 2
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Welcome to the Ranch!

When  a method is called, it "gains control" of what happens in the program. It keeps control until it explicitly returns via a return statement or implicitly returns after executing the last statement in it. When a method returns, execution continues from where it left off when the method was called. So, the method is said to "return" control to the code that called it.


Thank you junile for your explanation ,,,it is really helpful for me....
 
Junilu Lacar
Sheriff
Posts: 9338
96
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a simple example of an object whose state is changed by a method:

Only the first method, toggle(), changes the object's state. The other method simply reports on its state. This is why that explanation you cited is not good.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12296
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't be sure, but I think this may also be a holdover from C programming days.  you could start a program from a shell script or a perl script.  Then, depending on what the C program did, you may want to take different actions. Traditionally, that C program would return 0 if everything went according to plan, but would return some other numeric value if things went a bit astray.  there should have been some documentation (what we now call an API) that says something like

0 - good run
1 - source file not found
2- unable to write new file
etc.

So the main method in C could literally return a value to some other calling program, which could then branch accordingly.
 
Junilu Lacar
Sheriff
Posts: 9338
96
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what System.exit(int) is for, Fred
 
Carey Brown
Bartender
Posts: 2125
26
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:That's what System.exit(int) is for, Fred
The int supplied to exit() should be '0' to indicate success, non-zero otherwise (typically '1').
 
Junilu Lacar
Sheriff
Posts: 9338
96
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Junilu Lacar wrote:That's what System.exit(int) is for, Fred
The int supplied to exit() should be '0' to indicate success, non-zero otherwise (typically '1').

It should be noted that there are many nuances to calling System.exit(int).

Exiting normally from main() will set the system exit code to 0 anyway, even without explicitly calling System.exit(0), so you normally don't call it.

Consider the fact that calling System.exit() never returns normally. Also consider whether or not you may have non-daemon threads still running when you get to the end of main(). Then there is the connection to shutdown hooks and ensuring the proper release of resources.

And then you have specific OS conventions.

If you're running in a Windows environment, you might want to consider staying consistent with this: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
If you're running in a Linux environment, you might want to choose exit codes that are consistent with this: http://www.tldp.org/LDP/abs/html/exitcodes.html

System.exit() is kind of like Pandora's Box: if you look into it, all sorts of gnarly things can come out. So be careful and only mess with it if you know you really have to and only when there are exceptional circumstances you need to report back with an OS exit code.
 
Campbell Ritchie
Sheriff
Posts: 52001
90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An exmple of what Junilu means about a multi‑threaded environment: let us imagine you have thread 1 which has finished, whilst thread 2 has nearly finished and just has to write the results into a text file. Now you call exit(0) on thread 1 which you think is appropriate because thread 1 would have finished normally. Meanwhile thread 2 has written half the file and stops suddenly
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!