JVM 在哪里以及为什么检查入口方法 main(String args[]) 的返回类型是否为 void 而不是其他类型?
我会尝试回答这两个问题,如果我错了,请纠正我:
地点: 如果使用 Classname.method() 或反射调用静态方法,那么即使更改调用方法的返回类型也没关系,仍然会调用相同的方法。
因此 JVM 可能会在 jvm.cpp 的本机方法之一中检查这一点
方法句柄 m (THREAD, init_klass->find_method(vmSymbols::object_initializer_name(),>; vmSymbols::void_method_signature()));
if (m.is_null()) { ------ THROW_MSG_0 ………..
原因: 虽然从 main 返回值是没有用的,因为 java 不会对它做任何事情,但是如果我们尝试将 main 的返回类型更改为 int,例如,JVM 会抛出异常
公共静态int main(String[] args) { 返回1;
}java.lang.NoSuchMethodError:线程“main”中的主异常
因此,Java 可能要求入口方法 main() 使用相同的签名,以在所有方法中保持对称 Java程序编写。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,Java 中
main
返回void
的原因是线程。C 和 C++ 都是在多线程成为常见习惯之前设计的,而线程从其概念起就是 Java 的一个组成部分。在任何一种非平凡(多线程)程序中,都有多个线程,因此实际上您的程序永远不会从 main 的开始到结束线性运行。
由于 JVM 直到所有非守护线程都不会停止执行
运行完毕,从 main 方法返回并不意味着你的
节目结束。
考虑到这一点,
void
确实似乎是最适合 main 的返回类型。From what I can gather, the reason
main
returnsvoid
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.至于“为什么”:
我记得在过去的 Mac(OS 7 左右)上,Mac JVM 会接受不带任何参数的
static void main()
(因为 Mac 没有命令) -线)。现在已经不复存在了。我认为严格和明确的行为是有益的。否则,您最终会得到一些可以在某些平台上运行的程序,但由于相当愚蠢的原因而不能在其他平台上运行。正如您所指出的,来自
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.