• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return value from main

 
deepti krishna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The main method can be overloaded.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid if that overloaded main is understood by JVM ;)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic