为什么Java规范要求程序的主要方法只是无效? [英] Why does Java Specifications want the main method of a program to be void only?

查看:128
本文介绍了为什么Java规范要求程序的主要方法只是无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然返回类型不是方法签名的一部分,但JVM会查找确切的声明为

Although the return type is not a part of a method's signature, JVM looks for exact declaration as

 public static void main(String[] args)

我的假设是因为方法签名没有包含返回类型,我必须允许
更改返回类型。

My assumption is that since method signature does not have "return type" included, I must be allowed to change the return type.

但如果我将其更改为 public static int main(String [] args)并返回一个值,假设为0,JVM无法执行程序并退出并显示错误

But if I change it to public static int main(String[] args) and return a value lets say 0, JVM is not able to execute the program and exits with an error

Error: Main method must return a value of type void in class TestData, please 
define the main method as:
   public static void main(String[] args)

为什么Java规范要求main方法仅为空?
当返回类型不是方法签名的一部分时,为什么会出现此限制?

Why do Java Specifications want the main method to be void only? Why is this restriction, when return type is not a part of method signature?

此问题与为什么java中的main()无效?:这个问题因基于意见而被关闭,而我正在寻找代码放置/进程从JVM调用此方法的位置,以及强制它们将此方法保持为空的限制。他们采取了哪些设计决策(以及背后的推理),以及它在哪里记录。

This question is different than Why is main() in java void? : this question is closed as it is opinion based, while I am looking for the code places/ processes from where JVM is invoking this method, and what are the limitations which are forcing them to keep this method as void. What are the design decisions they have taken (and the reasoning behind), and where is it documented.

我正在寻找事实,以便知道原因是什么它这样做了。
请不要在没有详细说明问题的情况下将其标记为重复。

I am looking for facts, so as to know the reason why is it done so. Please don't mark it duplicate without going through the details of the question.

PS:退出,是另一回事。我更关心该计划的进入。此外,返回的值可能不是使用JVM,将其限制为void限制可扩展性。

PS: exit, is another thing. I am more concerned about entry of the program. Also, the returned value might not be to the use of JVM, restricting it to void limits extensibility.

到目前为止,我从这个问题中学到的是:Java规范有显式修复返回类型以避免混淆迁移程序员(C / CPP),他们可能希望此值返回到OS,但由于JVM介于此之间,因此该值永远不会返回到OS。出于这个特殊目的(返回操作系统的值),他们提供了System.exit()方法。

So far what I have learnt from this question is : Java specifications have explicitly fixed return type to avoid confusion to migrating programmers (C/CPP) who may expect this value to return to OS, but since JVM is in-between this value will never be returned to OS. For this special purpose (returning value to OS) they have provided System.exit() method.

对于所有那些人建议返回类型是签名的一部分 - 只是尝试在类中定义以下两种方法

For all those who are suggesting that return type is part of signature-- Just try to define both of the following methods in a class

    public static void main(String[] a){

    }
    public static String main(String[] a){

    }

您将收到编译错误,因为这两种方法的签名都相同。

You will get a compilation error, because signature of both of these methods is same.

推荐答案

main 为空/jls/se8/html/jls-12.html#jls-12.1.4\"rel =nofollow noreferrer> JLS 12.1.4调用 Test.main

The main is void is specified in JLS 12.1.4 Invoke Test.main:


方法 main 必须声明 public static void

有时回答为什么?因为规范是这样说的(又名Quod Ego Dixit,或者因为我这么说(Terry Pratchett))。有时语言设计师的决定是不可用的,我们唯一能做的就是猜测。这也是明显重复的原因(为什么java中的main()无效?)主要基于意见而被关闭。

Sometimes the answer to a why? is because the specification says so (aka Quod Ego Dixit, or Because I Say So (Terry Pratchett)). Sometimes the decisions of a language designer are not available and the only thing we can do is guess. This is also the reason the obvious duplicate (Why is main() in java void?) is closed as primarily opinion-based.

现在作为 - 对我来说 - 显而易见的原因: JLS 12.8程序退出说:

Now as the - to me - obvious reason: JLS 12.8 Program Exit says:


程序会终止其所有活动,并在发生以下两种情况之一时退出:

A program terminates all its activity and exits when one of two things happens:


  • 所有主题那些不是守护程序线程终止。

  • 某些线程调用类运行时的退出方法或类系统,安全管理员不禁止退出操作。

  • All the threads that are not daemon threads terminate.
  • Some thread invokes the exit method of class Runtime or class System, and the exit operation is not forbidden by the security manager.

这意味着 main 方法的结尾并不一定意味着程序的结束。来自 main 方法的返回码在C语言中具有众所周知的含义:它是进程退出代码。当 main 方法可能在进程本身结束之前很久就退出时,此含义不适用。

This means that the end of the main method does not necessarily mean the end of the program. A return code from a main method has a well-known meaning in languages like C: it is the process exit code. This meaning doesn't apply when the main method may exit long before the process itself ends.

大多数非平凡的Java应用程序通常都是一个短命的主要方法,可以启动一个或多个长期存在的非守护程序线程。允许来自Java main 方法的返回代码可能意味着实际上并不存在的含义:返回值是进程退出代码。因此明确指定 main 必须具有void返回类型才是好事:它可以减少混淆或分配不存在的含义。

Most non-trivial Java applications have - usually- a short-lived main method that kicks off one or more long-living non-daemon threads. Allowing a return code from a Java main method could imply a meaning that is not actually there: that the return value is the process exit code. So explicitly specifying that main must have a void return type is good thing: it reduces confusion or assigning meaning that isn't there.

这篇关于为什么Java规范要求程序的主要方法只是无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆