为什么 Java 中需要 String[] args?

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

Why is String[] args required in Java?

java

提问by Catharsis

I understand that String[] args is an array of strings passed into main as parameters.

我知道 String[] args 是作为参数传递给 main 的字符串数组。

java Print "Hello, World!"

 

 

class Print {
  public static void main(String[] args) {
    System.out.println(args[0]);
  }
}

However, when you don't include it as a parameter (even if you don't use), it throws an exception. So why is it required? Also, why can't it be an int[] or boolean[]?

但是,当您不将其作为参数包含时(即使您不使用),它也会引发异常。那么为什么需要它呢?另外,为什么不能是 int[] 或 boolean[]?

回答by Jon Skeet

It's a String because the command line is expressed in text. If you want to convert that text into integers or booleans, you have to do that yourself - how would the operating system or Java bootstrapper know exactly how you wanted everything to be parsed? I suppose Java couldlook for a main method with the same number of parameters as command line arguments, and then try to parse them using Integer.parseIntetc... but this would be pretty complicated, and would still be inadequate in many situations.

它是一个字符串,因为命令行是用文本表示的。如果您想将该文本转换为整数或布尔值,您必须自己完成 - 操作系统或 Java 引导程序如何确切地知道您希望如何解析所有内容?我想 Java可以寻找一个具有与命令行参数相同数量的参数的 main 方法,然后尝试使用Integer.parseInt等来解析它们......但这会非常复杂,并且在许多情况下仍然是不够的。

As for it being mandatory - basically the Java designers decided to stick to a single method signature, which is frankly simpler than allowing for it to be optional. It would be possible though - in C#, you can have any of

至于它是强制性的 - 基本上 Java 设计者决定坚持使用单个方法签名,坦率地说,这比允许它是可选的更简单。虽然有可能 - 在 C# 中,您可以拥有任何

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

Ultimately it doesn't make a lot of difference, to be honest. There's no significant downside in having to include the parameter.

说实话,最终它没有太大区别。必须包含参数没有明显的缺点。

回答by Greg Hewgill

The Java runtime system looks specifically for a method with a single String[]type parameter, because it wants to pass the parameters to your mainmethod. If such a method is not present, it informs you through an exception.

Java 运行时系统专门查找具有单个String[]类型参数的方法,因为它希望将参数传递给您的main方法。如果不存在这样的方法,它会通过异常通知您。

If you want to treat the (string) command line parameters as integers or booleans, you are expected to do the conversion yourself. This allows you to handle the condition where somebody enters "ponies"where you expect an integer.

如果您想将(字符串)命令行参数视为整数或布尔值,您需要自己进行转换。这允许您处理某人进入"ponies"您期望整数的情况。

回答by paxdiablo

That's how the language was designed. C and C++ both do it in a similar fashion, though with a separate count, information that's included in the string array for Java.

这就是语言的设计方式。C 和 C++ 都以类似的方式执行此操作,但使用单独的计数,即包含在 Java 字符串数组中的信息。

You couldhave designed it to take integers or booleans but that would have added more work to the Java runtime for potentially little benefit.

可以将其设计为采用整数或布尔值,但这会为 Java 运行时增加更多工作,但收益可能微乎其微。

Strings can be used to transmit any sort of information into the main program (including integers and booleans). Not so for integers if you want to pass in a string (short of the user having to manually encode it as UTF-8 or something, not something that would endear people to the language).

字符串可用于将任何类型的信息传输到主程序中(包括整数和布尔值)。如果你想传入一个字符串,整数就不是这样(用户必须手动将它编码为 UTF-8 或其他东西,而不是让人们喜欢这种语言的东西)。

Just remember to declare it. By all means, raise a change request on the language if you wish (to make it optional, or allow other signatures) but I suspect that will not get very far.

只记得声明它。无论如何,如果您愿意(使其成为可选的,或允许其他签名),可以对语言提出更改请求,但我怀疑这不会走得太远。

回答by Bostone

It's a convention. The method is used by command line interpreter and that's how it expects it. Moreover - the compiler expects the signature in this particular form too and won't compile if you omit the parameter, for example

这是一个约定。该方法由命令行解释器使用,这就是它所期望的。此外 - 编译器也期望这种特定形式的签名,如果省略参数,则不会编译,例如

回答by Nitin Dhir

The string[] parameter is used to save command line arguments with first being the class name. Moreover,if you call your main method without string[] it will simply be an overload and JVM will not be calling that method.And if you skip this signature main method JVM will throw an exception

string[] 参数用于保存命令行参数,首先是类名。此外,如果你在没有 string[] 的情况下调用你的 main 方法,它只会是一个重载,JVM 不会调用那个方法。如果你跳过这个签名 main 方法,JVM 将抛出异常

回答by bdonlan

Because the code that runs your main()function is looking very specifically for that signature. Think of it as having, somewhere:

因为运行您的main()函数的代码非常专门针对该签名。把它想象成在某个地方:

YourClass.main(new String[] { ... });

Obviously, if main takes anything else, this will fail.

显然,如果 main 接受其他任何东西,这将失败。