的返回类型是否为void

的返回类型是否为void

本文介绍了JVM在哪里和为什么检查入口方法main(String args [])的返回类型是否为void,而不是其他值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我会尽量回答两个问题,如果我错了,请更正我: 其中: 如果使用静态方法正在使用 Classname .method()或使用反射调用,即使更改调用方法的返回类型也没关系,仍将调用相同的方法。 $所以JVM可能在 jvm.cpp 的原生方法之一中检查这个所以可能是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.cppWhy: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 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 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 threadshave finished running, returning from the main method doesn't mean yourprogram ended.With that in mind, void indeed seems like the most suited return type for main. 这篇关于JVM在哪里和为什么检查入口方法main(String args [])的返回类型是否为void,而不是其他值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 12:30