Where and why JVM checks that the return type of entry method main(String args[]) is void and not anything else?

这一生的挚爱 提交于 2019-11-29 15:22:49

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!