为什么 Java main() 方法接受 String args 数组?

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

Why does Java main() method accept an array of String args?

javaprogramming-languages

提问by Click Upvote

Since its possibly one of the most widely used methods of the Java language, why does it have to accept an array of Strings and doesn't work without it? For example, I could always live with:

既然它可能是 Java 语言中使用最广泛的方法之一,为什么它必须接受字符串数组并且没有它就不能工作?例如,我可以一直生活在:

public static void main() {}

over

超过

public static void main(String[] args) {}

Is there a higher purpose to this than just being able to accept command-line arguments, especially since a vast majority of Java programs are GUI driven and don't need to receive args through command line?

除了能够接受命令行参数之外,这是否还有更高的目的,特别是因为绝大多数 Java 程序都是 GUI 驱动的,不需要通过命令行接收参数?

回答by Jon Skeet

I agree that it could be more flexible. In C# for instance, all of these are acceptable entry points:

我同意它可以更灵活。例如,在 C# 中,所有这些都是可接受的入口点:

static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)

The versions returning intuse the return value as the exit code for the process. If you don't care about receiving any arguments, you can use one of the versions which doesn't take any. Any arguments given when the program is launched are ignored.

返回的版本int使用返回值作为进程的退出代码。如果您不关心接收任何参数,则可以使用不带任何参数的版本之一。程序启动时给出的任何参数都将被忽略。

So yes, it couldbe more flexible - but it's never struck me as a significant problem in Java.

所以是的,它可以更灵活 - 但它从来没有让我觉得它是 Java 中的一个重大问题。

回答by Peter

even a gui driven java app will start with some main method. The "higher purpose" has never been to accept command line arguments.

即使是 gui 驱动的 java 应用程序也会从一些主要方法开始。“更高的目的”从来都不是接受命令行参数。

The purpose is just to accept arguments. Period. Whenever you start any program not just Java you will always need some syntax to pass arguments

目的只是接受参数。时期。无论何时启动任何程序,而不仅仅是 Java,您总是需要一些语法来传递参数

回答by deets

Programs alwaystake commandline arguments. It's up to the programmer to decide if they are used for anything.

程序总是采用命令行参数。由程序员决定它们是否用于任何用途。

So implementing a main without a string-array would lead to more complex startup-logic and potentially confusing and errorneous behavior, for the more than debatable gain of not writing a single parameter declaration less (in your whole program!)

因此,在没有字符串数组的情况下实现 main 将导致更复杂的启动逻辑和潜在的混乱和错误行为,因为不编写单个参数声明(在整个程序中!)

And given the overall verbosity of Java & the support for common templates for boilerplate code in IDEs like Eclipse, I fail to see where that's really an issue.

考虑到 Java 的整体冗长性以及对 Eclipse 等 IDE 中样板代码通用模板的支持,我看不出这到底是哪里出了问题。

回答by boj

"Is there a higher purpose to this (..)"

“这有没有更高的目的(..)”

Yes: legacy.

是的:遗产。

回答by dommer

Lots of GUI programs provide facilities to accept command-line switches to control their behaviour at start up.

许多 GUI 程序提供了接受命令行开关的工具,以控制它们在启动时的行为。

回答by Mehrdad Afshari

What "vast-majority" of programs do does not really make much difference in terms of what is needed to be done. There are still lots of command line programs that are driven by command line args.

就需要完成的工作而言,“绝大多数”程序所做的实际上并没有太大区别。仍然有很多命令行程序是由命令行参数驱动的。

回答by Edmund

I guess it's because many Java programs will take at least some parameters. Like C (a syntactical inspiration of Java if nothing else), Java considers the best way to provide these as parameters to main.

我想这是因为许多 Java 程序至少会采用一些参数。与 C 一样(如果没有别的的话,这是 Java 的语法灵感),Java 认为将这些作为参数提供给main.

Even a GUI program will often take command line parameters. Just because the shell it's launched from typically doesn't ask for these parameters (by default), doesn't mean they're not supported.

即使是 GUI 程序也会经常使用命令行参数。仅仅因为它启动的 shell 通常不要求这些参数(默认情况下),并不意味着它们不受支持。

回答by dfa

there are also many java programs that receive args through command line. Think of javac, ant, maven, etc

还有很多java程序通过命令行接收args。想想javac、ant、maven等