什么是正确的 Java main() 方法参数语法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4089572/
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
What is the correct Java main() method parameters syntax?
提问by james
Is there a functional difference between these methods?
这些方法之间是否存在功能差异?
public static void main(String[] args) { }
public static void main(String args[]) { }
EDIT (added this syntax from other contributors) :
编辑(从其他贡献者那里添加了这个语法):
public static void main(String... args) { }
回答by starblue
No, but the first is the prefered style.
不,但第一个是首选样式。
Edit:Another option is
编辑:另一种选择是
public static void main(String... args)
which additionally allows callers to use varargs syntax.
它还允许调用者使用可变参数语法。
回答by Sean Patrick Floyd
Different Array notations
不同的数组符号
The notation
符号
String args[]
is just a convenience for C programmers, but it's identical to this notation:
只是为 C 程序员提供方便,但它与此表示法相同:
String[] args
Here's what the Sun Java Tutorial says:
这是 Sun Java 教程所说的:
You can also place the square brackets after the array's name:
float anArrayOfFloats[]; // this form is discouraged
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
您还可以将方括号放在数组名称之后:
float anArrayOfFloats[]; // this form is discouraged
然而,惯例不鼓励这种形式;括号标识数组类型,并应与类型名称一起出现。
Reference:Java Tutorial > Arrays
参考:Java 教程 > 数组
VarArgs
可变参数
BTW, a lesser known fact is that main methods also support varargs, so this is also okay:
顺便说一句,一个鲜为人知的事实是主要方法也支持可变参数,所以这也可以:
public static void main(String ... args) { }
The reason is that a varargs method is internally identical to a method that supports a single array parameter of the specified type.E.g. this won't compile:
原因是 varargs 方法在内部与支持指定类型的单个数组参数的方法相同。例如,这不会编译:
public static void main(final String... parameters){}
public static void main(final String[] parameters){}
// compiler error: Duplicate method main(String[])
回答by Frank
The other answers are correct in that it doesn't make a difference. Let me just add two further points:
其他答案是正确的,因为它没有区别。我再补充两点:
String ... args
is also valid now and in this case again makes no difference.
String ... args
现在也是有效的,在这种情况下再次没有区别。
The different options to place your brackets do have a consequence, when you define multiple variables. In the following example the variable a
is not a String array, but b
is one and c
is an array of arrays of Strings.
当您定义多个变量时,放置括号的不同选项确实会产生影响。在以下示例中,变量a
不是字符串数组,而是b
一个,并且c
是字符串数组的数组。
String a, b[], c[][];
String a, b[], c[][];
However, I have to suggest not to use this style for your code, as it can quickly become very confusing. For example, String [] b, c[];
means the same for b
and c
as above, but especially for c
this is non-obvious.
但是,我必须建议不要在您的代码中使用这种样式,因为它很快就会变得非常混乱。例如,String [] b, c[];
forb
和c
as above 的含义相同,但特别是对于c
this 是不明显的。
回答by Vivin Paliath
There is no difference, but the first one is according to standard.
没有区别,但第一个是根据标准。
回答by Cristian
No, they have no difference. Though... I used to use the second way, until my girlfriend threatened to break if I continued doing it (not kidding). So now I prefer the first way, and I think it looks much better.
不,它们没有区别。虽然......我曾经使用第二种方式,直到我的女朋友威胁说如果我继续这样做就会破坏(不是开玩笑)。所以现在我更喜欢第一种方式,我认为它看起来好多了。
回答by Joel
No, the above are equivalent. Arrays in Java can be declared in one of two ways, either:
不,以上是等价的。Java 中的数组可以通过以下两种方式之一声明:
String[] myarray;
or
或者
String myarray[];
回答by pyCoder
Use String[] instead of use [] postfix to reference. Infact String[] obj; hilights the fact that obj is a reference of type String[] (array of String).
使用 String[] 而不是使用 [] 后缀来引用。事实上 String[] obj; 突出了 obj 是 String[] 类型(字符串数组)的引用这一事实。
回答by dogbane
I also prefer to mark the args
as final
.
我也更喜欢将其标记args
为final
。
public static void main(final String[] args) { }
回答by Divya Patel
There is not a difference between the 2 choices you can do either one of them.
您可以选择其中任何一个,这两种选择之间没有区别。
You could also put this:
你也可以这样写:
public static void main(String... args) { }