java中main的返回类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2441036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:41:16  来源:igfitidea点击:

return type of main in java

java

提问by giri

I like to know why only void return type for main method in java.

我想知道为什么 java 中的 main 方法只有 void 返回类型。

public static void main(String [] args)

Why there is no other return types other than void for main method.

为什么 main 方法除了 void 没有其他返回类型。

Thanks

谢谢

回答by Stephen C

Firstly, because there is nothing to return values to. Mainstream operating systems (e.g. Windows, UNIX, Linux, etc) do not allow commands to "return" values. (Though arguably, the exit code is a return value, but it is restricted to an integer and there are various OS-specific caveats; e.g. on how many of the bits can come from the application.)

首先,因为没有什么可以返回值。主流操作系统(例如 Windows、UNIX、Linux 等)不允许命令“返回”值。(虽然可以说,退出代码是一个返回值,但它仅限于一个整数,并且有各种特定于操作系统的警告;例如,有多少位可以来自应用程序。)

Secondly, because Java does not allow you to overload methods that differ only on the return type.

其次,因为 Java 不允许重载仅在返回类型上不同的方法。

Thirdly, as @Tom points out, if the exit codewas the return value of main, it would be difficult to deal with other threads calling System.exit(rc).

第三,正如@Tom 所指出的,如果exit code是 的返回值main,则很难处理其他线程调用System.exit(rc).

回答by polygenelubricants

Java specifies that an application entry point must be public static, named main, returns void, and takes a String[]as a parameter. This is just the way it is.

Java 指定应用程序入口点必须是public static、已命名main、返回void并且将 aString[]作为参数。这就是它的方式。

That said, you cantechnically define a mainmethod that doesn't meet those criterias, but they won't be a valid application entry point.

也就是说,您可以在技术上定义main不符合这些标准的方法,但它们不会是有效的应用程序入口点

public class MainMain {
    public static void main(String args[]) { // application entry point
        System.out.println(main());
    }
    private static String main() {           // just a method named main
        return "Hello world!";
    }
}

You should probably reserve mainonly for valid Java application entry points, but there's nothing preventing you from writing a code like the above.

您可能应该main只保留有效的 Java 应用程序入口点,但没有什么可以阻止您编写上述代码。

回答by Greg Hewgill

The short answer is: Because that's what the language specificationsays.

简短的回答是:因为这就是语言规范所说的。

In today's commonly used operating systems (Windows and Unix families), the only "return value" from a process is an exit status, which is a small number (usually 8-bit). This value can be returned from a Java program using the System.exit(int)method:

在当今常用的操作系统(Windows 和 Unix 系列)中,进程唯一的“返回值”是exit status,它是一个很小的数字(通常为 8 位)。可以使用以下System.exit(int)方法从 Java 程序返回此值:

public static void exit(int status)

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

public static void exit(int status)

终止当前运行的 Java 虚拟机。参数用作状态代码;按照惯例,非零状态代码表示异常终止。

回答by Tom Hawtin - tackline

What were you expecting to return?

你期待返回什么?

In C you typically return an intexit code. However, in a multithreaded system this doesn't make much sense. Typically a Java program will start a number of threads. The main thread will often return more or less immediately.

在 C 中,您通常会返回一个int退出代码。但是,在多线程系统中,这没有多大意义。通常,Java 程序会启动多个线程。主线程通常或多或少会立即返回。

System.exitcan be used to exit with a specific exit code.

System.exit可用于以特定的退出代码退出。

回答by Devraj Jaiman

Because you run main from JVM Hello.main, we can't return value to JVM. It simply gives run time exception.

因为您从 JVM 运行 main Hello.main,所以我们无法将值返回给 JVM。它只是给出运行时异常。

回答by necromancer

It seems like an arbitrary decision, probably considering that not all operating systems are unixy in the sense that they allow a program to return an exit code.

这似乎是一个武断的决定,可能是考虑到并非所有操作系统都是 unixy,因为它们允许程序返回退出代码。

Java could have been specified like C with an intreturn code from main. Multi-threading does not matter because there is only one thread that originally starts main and its return value could be the one to be returned to the OS by the JVM, regardless of when the other threads return and what they do.

Java 可以像 C 一样使用int来自main. 多线程无关紧要,因为只有一个线程最初启动 main 并且其返回值可能是 JVM 返回给操作系统的那个,而不管其他线程何时返回以及它们做了​​什么。

This is far-fetched but if someone built a JVM that can run multiple Java programs each with its own main, it could be designed to return the value returned by the last mainto be invoked.

这是牵强附会的,但如果有人构建了一个 JVM,该 JVM 可以运行多个 Java 程序,每个程序都有自己的main,它可以设计为返回最后main调用的返回值。

The most reasonable rationale is that the JVM itself may need to return an exit code for reasons of its own, however, this rationale is belied by the provision of System.exit(int)which exactly overrides the JVM's return code. Given this, there is no reason to similarly allow mainto return an inttoo, just as if the last thing that maindid was System.exit(n)

最合理的理由是 JVM 本身可能出于其自身的原因需要返回退出代码,但是,该理由被System.exit(int)完全覆盖 JVM 的返回代码的规定所掩盖。鉴于此,没有理由同样允许main返回 an inttoo,就好像最后一件事mainSystem.exit(n)

All in all, sounds like an arbitrary decision.

总而言之,听起来像是一个武断的决定。

回答by samantp

A reasonable middle way (between having a void signature, but also needing to return a value to the OS) to me then seems to be: To throw a Runtime exception in main for all return values that correspond to an error, with an appropriate message in that exception.

对我来说,一种合理的中间方式(在具有无效签名之间,但也需要向操作系统返回一个值)似乎是:在 main 中为所有与错误对应的返回值抛出一个运行时异常,并带有适当的消息在那个例外。

Of course, this helps only to differentiate between error and non-error exits at the OS level (say, a shell script etc.) but in many cases, this is actually all that is needed, and probably the most common use of these environment return values; either run or don't run the next command if this command fails.

当然,这仅有助于区分操作系统级别的错误退出和非错误退出(例如,shell 脚本等),但在许多情况下,这实际上就是所需要的,并且可能是这些环境最常见的用途返回值;如果此命令失败,请运行或不运行下一个命令。