aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return value from main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return value from main" Watch "return value from main" New topic
Author

return value from main

deepti krishna
Greenhorn

Joined: Jun 06, 2009
Posts: 3
Hello all
am a beginner in java

my doubts are as below

1.. where does the main return the value to?
2... can we capture the value returned by main? if yes how do we do it? can someone provide with an example?

Thanks in advance
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

deepti krishna wrote:1.. where does the main return the value to?

main method doesn't return any value, because its return type is void

deepti krishna wrote:2... can we capture the value returned by main? if yes how do we do it? can someone provide with an example?

I don't think we can capture the returned value(??) from main, If there is any way, then need to move this post to JiG


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Harsh Pensi
Ranch Hand

Joined: Aug 05, 2009
Posts: 67


The main method can be overloaded.


SCJP6 - 93% SCWCD5 - 97%
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

I'm afraid if that overloaded main is understood by JVM ;)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

Murtuza Arzai wrote:The main method can be overloaded.

It can be overloaded, because it's just a normal method just like any other method. But you can't make the JVM start the execution of your program with an overloaded version of main() - it will always look for a main() method that is public, static, void, and takes a String[] as input.

As already said, the normal main method does not return any value. You can however end your program by calling System.exit(int). If you call your Java program from a Windows BAT file or Unix shell script, then you can get the value that you pass to System.exit in the BAT file or script.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Jesper Young wrote:You can however end your program by calling System.exit(int). If you call your Java program from a Windows BAT file or Unix shell script, then you can get the value that you pass to System.exit in the BAT file or script.

I didn't know that, thanks Jasper. System.exit(int) - an abrupt exit return
sireesha vadlamani
Greenhorn

Joined: Aug 10, 2014
Posts: 20
Sagar Rohankar wrote:
deepti krishna wrote:1.. where does the main return the value to?

main method doesn't return any value, because its return type is void

deepti krishna wrote:2... can we capture the value returned by main? if yes how do we do it? can someone provide with an example?

I don't think we can capture the returned value(??) from main, If there is any way, then need to move this post to JiG

if we mention it as an int return type then is the main() method return the value???
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

sireesha vadlamani wrote:if we mention it as an int return type then is the main() method return the value???

If you declare a method called "main" that returns an int, that specific method will not be run when you start your program.

That does not prevent you from calling it explicitly in your program, but the value will be returned to whatever called it, not to the OS.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

sireesha vadlamani wrote:
if we mention it as an int return type then is the main() method return the value???


Bumping an old topic, when you don't seem to be getting an answer from your other topic is not going to help you here -- especially since the answer is "it doesn't work that way". See Fred's response -- to both topics.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return value from main