JVM在哪里和为什么检查入口方法main(String args [])的返回类型是否为void,而不是其他值? [英] Where and why JVM checks that the return type of entry method main(String args[]) is void and not anything else?
问题描述
我会尽量回答两个问题,如果我错了,请更正我:
其中:
如果使用静态方法正在使用 Classname .method()或使用反射调用,即使更改调用方法的返回类型也没关系,仍将调用相同的方法。
methodHandle m(THREAD,
init_klass-> find_method(vmSymbols :: object_initializer_name(),>
vmSymbols :: void_method_signature()));
if(m.is_null()){
------ THROW_MSG_0 ...........
< blockquote>
为什么:
虽然从main中返回一个值是没用的,因为java不会做任何事情,但如果我们试图改变返回类型为main to int例如JVM throws
public static int main )
{
return 1;
}
java.lang.NoSuchMethodError:main线程中的异常 main
所以可能是Java强制使用相同的签名方法main >对称。
解决方案从我可以收集的原因,
main
在Java中返回void
是线程。
C和C ++都设计在多线程之前是一个常见的成语,而线程是Java从其概念的一个组成部分。在任何类型的非平凡(多线程)程序中,有多个线程,因此在现实中,你的程序永远不会从主线程的开始到结束线性运行。
由于JVM不会停止执行,直到所有非守护进程线程
完成运行,从main方法返回并不意味着
程序结束。
考虑到这一点,
void
确实看起来像是main的最适合的返回类型。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.
解决方案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.这篇关于JVM在哪里和为什么检查入口方法main(String args [])的返回类型是否为void,而不是其他值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!