views:

118

answers:

3

I will try to answer both, please correct me if I am wrong:

Where: If a static method is being called using Classname.method() or using reflection then it doesn’t matter even if you change the return type of the calling method, the same method will still be called.

So JVM probably checks this in one of the native methods of jvm.cpp

methodHandle m (THREAD, init_klass->find_method(vmSymbols::object_initializer_name(),> vmSymbols::void_method_signature()));

if (m.is_null()) { ------ THROW_MSG_0 ………..

Why: Although its useless to return a value from main, as java does not do anything with it but if we try to change the return type of main to int for example, JVM throws

public static int main(String[] args) { return 1;
}

java.lang.NoSuchMethodError: main Exception in thread "main"

So may be Java mandates the use of same signature for entry method main() to maintain a symmetry in all Java programs written.

A: 

As to "why":

I remember in the olden days on the Mac (OS 7 or so), the Mac JVM would accept a static void main() without any args (because the Mac had no command-line). That is gone now.

I suppose strict and unambigious behaviour is beneficial. Otherwise you'd end up with programs that work on some platforms and not on others for rather silly reasons. As you point out, any return value from main is discarded anyway.

Thilo
@Thilo In java, return value from main is not discarded rather it doesn't allow that you return any value from main.
akjain
I am still waiting for answer to "Where".
akjain
A: 

What is the reason to return value from main() method?

Artic
In C programs on Unix systems (and probably Windows) the return value of main is the error code returned from the command (within the limits afforded return codes on the various platforms, of course). Similar thoughts could apply to Java.
JUST MY correct OPINION
On unix systems each programm has a return code, so this would be a more convenient way to return such a code instead of using Runtime.exit().
Arne Burmeister
+1  A: 

From what I can gather, the reason main returns void in Java is threads.

C and C++ were both designed before multithreading was a common idiom, while threads were an integral part of Java from its conception. In any kind of non-trivial (multi-threaded) program, there is more than one thread, and so in reality your program never runs linearly from start to end of main.

Since the JVM doesn't halt execution until all non-daemon threads have finished running, returning from the main method doesn't mean your program ended.

With that in mind, void indeed seems like the most suited return type for main.

JRL
@JRL (main returns void, reason is threads) Can you elaborate this a bit more, or point me to any link where I can read more about this concept.
akjain
@akjain: I've read it in multiple places, including stackoverflow and velocityreviews. I don't have authoritative links like a book or standard, though.
JRL
@akjain: another example: the default desktop app template in NetBeans. All the main method does is new the GUI class in the EDT thread, then exits immediately. The app is then still running... would returning a value from main then make any sense?
JRL