java中的main()为什么是空的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/540396/
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-11 15:59:16  来源:igfitidea点击:

Why is main() in java void?

java

提问by

In the case of languages with a C-like syntax, we declare the main() method to return an int or float value (or void). Is it possible to declare a non-void return type from main() in Java? If not, then why not? Does this mean that a Java program doesn't return any value to the OS?

对于具有类似 C 语法的语言,我们声明 main() 方法以返回 int 或 float 值(或 void)。是否可以在 Java 中从 main() 声明非空返回类型?如果没有,那为什么不呢?这是否意味着 Java 程序不会向操作系统返回任何值?

回答by starblue

You can return an int with System.exit().

您可以使用 System.exit() 返回一个 int。

Returning anything other than an integer doesn't make much sense, as the OS expects an integer. In case nothing is returned the default is 0, which means OK. Other values typically are used to signal errors or special conditions.

返回整数以外的任何内容都没有多大意义,因为操作系统需要整数。如果没有返回任何内容,则默认值为 0,这意味着 OK。其他值通常用于表示错误或特殊情况。

回答by Nicolai

Thisis an interesting discussion on velocityreviewson the same topic:

velocityreviews关于同一主题的有趣讨论:

Highlight:

强调:

Incidentally, this is considered bad style in C and C++ just because it's the wrongsignature for main, not for any universal reason independent of programming languages. It's one of those things that is not really supposed to work, but might on your implementation.

In Java, the reason main returns void is threads. C and C++ were both designed as languages before multithreading was a widely known technique, and both had threads grafted onto them at a later date. Java was designed from the beginning to be a multithreaded environment, and frankly, it would be unusual to write any non-trivial Java application that doesn't use more than one thread. So the idea that a program moves linearly from the beginning to the end of main is a bit outdated.

顺便说一句,这在 C 和 C++ 中被认为是不好的风格,只是因为它是main的错误签名,而不是出于任何独立于编程语言的普遍原因。这是不应该真正起作用的事情之一,但可能会影响您的实施。

在 Java 中,main 返回 void 的原因是线程。在多线程成为广为人知的技术之前,C 和 C++ 都被设计为语言,并且后来都将线程移植到它们上面。Java 从一开始就被设计为多线程环境,坦率地说,编写任何不使用多个线程的非平凡 Java 应用程序是不寻常的。因此,程序从 main 的开头到结尾线性移动的想法有点过时了。

written by

写的

www.designacourse.com The Easiest Way to Train Anyone... Anywhere. Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation

www.designacourse.com 培训任何人的最简单方法……任何地方。Chris Smith - 首席软件开发人员/技术培训师 MindIQ Corporation

回答by Zach Scrivena

The main()method must indeed have a voidreturn type. From the Java Language Specificationon "Execution - Virtual Machine Start-Up" (§12.1.4):

main()方法确实必须具有void返回类型。来自“执行 - 虚拟机启动”的Java 语言规范(第12.1.4 节):

The method mainmust be declared public, static, and void. It must accept a single argument that is an array of strings.

该方法main必须声明 publicstaticvoid。它必须接受作为字符串数组的单个参数。

It goes on to describe when a program exits in "Execution - Program Exit" (§12.8):

它继续在“执行 - 程序退出”(第12.8 节)中描述程序何时退出:

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

  • All the threads that are not daemon threads terminate.
  • Some thread invokes the exitmethod of class Runtimeor class Systemand the exit operation is not forbidden by the security manager.

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

  • 所有不是守护线程的线程都将终止。
  • 某些线程调用exitRuntime或类的方法, System安全管理器不禁止退出操作。

In other words, the program may exit before or after the mainmethod finishes; a return value from mainwould therefore be meaningless. If you want the program to return a status code, call one of the following methods (note that all three methods never return normally):

换句话说,程序可能会在main方法完成之前或之后退出;main因此,来自的返回值将毫无意义。如果您希望程序返回状态代码,请调用以下方法之一(请注意,所有三种方法都不会正常返回):

Of the three, System.exit()is the conventional and most convenient way to terminate the JVM.

三者中,System.exit()是终止 JVM 的常规和最方便的方法。

回答by Peter ?tibrany

The reason for the mainmethod having voidas return type is that once mainfinishes, it doesn't necessarily mean that the entire program finished. If mainspawns new threads, then these threads can keep program running. The return type of maindoesn't make much sense at this point.

main方法具有void作为返回类型的原因是,一旦main完成,并不一定意味着整个程序都完成了。如果main产生新线程,则这些线程可以保持程序运行。的返回类型main在这一点上没有多大意义。

For example, this is very common in Swing applications, where the mainmethod typically starts a GUI on the Swing thread, and then mainfinishes... but the program is still running.

例如,这在 Swing 应用程序中很常见,该main方法通常在 Swing 线程上启动 GUI,然后main完成……但程序仍在运行。