简体   繁体   English

main(String args[]) 和 main(String[] args) 之间有区别吗?

[英]Is there a difference between main(String args[]) and main(String[] args)?

Is there a difference between:是否有区别:

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

and

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

I don't believe so, but I am wondering.我不相信,但我想知道。

Semantically, they are identical.在语义上,它们是相同的。 However, I'd recommend using the latter syntax ( String[] args ) when declaring arrays.但是,我建议在声明数组时使用后一种语法( String[] args )。 The former syntax is there mainly for compatibility with C syntax.前一种语法主要是为了与 C 语法兼容。

Since String[] , as a whole, is the type of the object in Java, it's more consistent and clear not to split it up.由于String[]整体而言是 Java 中对象的类型,因此不拆分它会更加一致和清晰。

A similar question addresses the [] after method argument list.一个类似的问题解决了方法参数列表之后的[]

没有区别,但将括号放在类型 ( String[] ) 之后是 Java 中更常见的做法。

Both of them are absolutely the same.两者完全相同。 Please refer to Java Language Specification (JLS) to see the syntax used in java.请参阅 Java 语言规范 (JLS) 以查看 java 中使用的语法。

String[] args or String args[] will create an array (reserves a place in memory) with no size and name args . String[] argsString args[]将创建一个没有大小和名称args的数组(在内存中保留一个位置)。

Let us Consider:让我们考虑一下:

String [] x;

Putting the [] after the name avoids subtle problems like this:-[]放在名称后面可以避免这样的微妙问题:-

String x [] , y; //x[] is an array but y is a String
String [] x y ; //two arrays x[] and y[] both

Although both are used to create array of String type but second one is more popular, because with that, you can create multiple String arrays simultaneously... Eg String[] a,b;虽然两者都用于创建 String 类型的数组,但第二种更受欢迎,因为使用它,您可以同时创建多个 String 数组... 例如String[] a,b; Here you have created two String Arrays a and b simultaneously.在这里,您同时创建了两个字符串数组 a 和 b。

However然而

String a[],b; will create String array "a" and String variable(not array) b .将创建字符串数组 "a" 和字符串变量(不是数组) b

Hope this clarifies.希望这能澄清。

No

They are just two style of writting它们只是两种写作风格

The method signature is the same so there is no difference.方法签名是相同的,所以没有区别。

Its a public method, it returns nothing, the method name is "main" and the it takes a String array.它是一个公共方法,它不返回任何内容,方法名称是“main”,它采用一个 String 数组。

The difference between these is that when we try to call the main method manually from one class to another by using static syntax , we use "string[] s ".它们之间的区别在于,当我们尝试使用静态语法从一个类手动调用 main 方法到另一个类时,我们使用“string[] s”。 At that point we have to pass an array of string type as argument in main method.那时我们必须在 main 方法中传递一个字符串类型的数组作为参数。 When we are using "String... s" then there is no need to pass any string array as an argument.当我们使用 "String... s" 时,不需要将任何字符串数组作为参数传递。 It will run and it doesn't matter if you pass the array as an argument or not (if you don't explicitly pass it, it is passed by itself) ... hence proved/////////它将运行,如果您将数组作为参数传递与否无关紧要(如果您没有明确传递它,它会自行传递)......因此证明//////////

Nope.不。 There is no difference b/w the two.两者没有区别。

See Declaring a Variable to Refer to an Array section in this doc .Declaring a Variable to Refer to an Array 本文档中的Declaring a Variable to Refer to an Array部分。 From that doc从那个文档

float anArrayOfFloats[];浮动 anArrayOfFloats[]; // this form is discouraged // 这种形式是不鼓励的

so just use the second style.所以只需使用第二种样式。

Apart from these two styles, it is worth mentioning that the following is also a valid signature for the main() method since Java 5:除了这两种风格,值得一提的是,以下也是自 Java 5 以来main()方法的有效签名:

public static void main(String... args)

From the Java Language specification :Java 语言规范

The method main must be declared public, static, and void.方法 main 必须声明为 public、static 和 void。 It must specify a formal parameter (§8.4.1) whose declared type is array of String.它必须指定一个形式参数(第 8.4.1 节),其声明的类型是字符串数组。

The ellipsis ... is the syntax for var-args, which can be used to pass an arbitrary number of arguments to a method.省略号...是 var-args 的语法,可用于将任意数量的参数传递给方法。 More on it here .更多关于它在这里

String args[] and String[] args are identical. String args[] 和 String[] args 是相同的。 In the sense that they do the same thing, Creating a string array called args.从某种意义上说,他们做同样的事情,创建一个名为 args 的字符串数组。

But to avoid confusion and enforce compatibility with all other Java codes you may encounter I'd recommend using the syntax (String[] args) when declaring arrays.但是为了避免混淆并强制与您可能遇到的所有其他 Java 代码兼容,我建议在声明数组时使用语法 (String[] args)。

One confusion you may encounter is when you try to declare multiple String array in one line, then String[] fruits, vegetables will not be the same as String fruits[], vegetables您可能会遇到的一种困惑是,当您尝试在一行中声明多个 String 数组时,那么String[] fruits, vegetables将与String fruits[], vegetables

The former creates two string arrays fruits and vegetables , while the later creates a String array of fruits and a string variable of vegetables前者创建了两个字符串数组fruits蔬菜,而后者创建了一个水果字符串数组和一个蔬菜字符串变量

there is a difference: For example if you write int[] arr;有区别:例如,如果你写 int[] arr; - here arr is the reference to the integer array whereas in case of int arr[]; - 这里 arr 是对整数数组的引用,而在 int arr[] 的情况下; - arr is a primitive int type array - arr 是一个原始的 int 类型数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM