main(String args[]) 和 main(String[] args) 之间有区别吗?
之间有区别吗:
public void main(String args[]) { ... }
和
public void main(String[] args) { ... }
我不相信,但我想知道。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
从语义上来说,它们是相同的。但是,我建议在声明数组时使用后一种语法 (
String[] args
)。前者的语法主要是为了兼容C语法。由于
String[]
作为一个整体,就是Java中对象的类型,所以不拆分它会更加一致和清晰。类似问题解决了方法参数列表之后的
[]
。Semantically, they are identical. However, I'd recommend using the latter syntax (
String[] args
) when declaring arrays. The former syntax is there mainly for compatibility with C syntax.Since
String[]
, as a whole, is the type of the object in Java, it's more consistent and clear not to split it up.A similar question addresses the
[]
after method argument list.没有什么区别,但在类型后面加上括号 (
String[]
) 是 Java 中更常见的做法。There's no difference, but putting the brackets after the type (
String[]
) is the more common practice in Java.两者完全一样。请参阅Java语言规范(JLS)来查看java中使用的语法。
String[] args
或String args[]
将创建一个没有大小和名称args
的数组(在内存中保留一个位置)。让我们考虑一下:
将
[]
放在名称后面可以避免像这样的微妙问题:-Both of them are absolutely the same. Please refer to Java Language Specification (JLS) to see the syntax used in java.
String[] args
orString args[]
will create an array (reserves a place in memory) with no size and nameargs
.Let us Consider:
Putting the
[]
after the name avoids subtle problems like this:-虽然两者都用于创建 String 类型的数组,但第二种更流行,因为使用它,您可以同时创建多个 String 数组...例如
String[] a,b;
这里您同时创建了两个字符串数组 a 和 b。
然而,
String a[],b;
将创建字符串数组“a”和字符串变量(不是数组)b。希望这能澄清。
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... E.g
String[] a,b;
Here you have created two String Arrays a and b simultaneously.
However
String a[],b;
will create String array "a" and String variable(not array) b.Hope this clarifies.
不
它们只是两种写作风格
No
They are just two style of writting
方法签名相同,因此没有区别。
它是一个公共方法,它不返回任何内容,方法名称是“main”,它接受一个字符串数组。
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.
没有。两者没有任何区别。
请参阅 声明变量以引用数组部分>本文档。从那个文档
所以只使用第二种风格。
Nope. There is no difference b/w the two.
See
Declaring a Variable to Refer to an Array
section in this doc. From that docso just use the second style.
它们之间的区别在于,当我们尝试使用静态语法从一个类手动调用 main 方法到另一个类时,我们使用“string[] s”。此时我们必须在 main 方法中传递一个字符串类型的数组作为参数。当我们使用“String...s”时,不需要传递任何字符串数组作为参数。它将运行,无论您是否将数组作为参数传递(如果您没有显式传递它,它都会自行传递)...
因此证明/////////
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 ". At that point we have to pass an array of string type as argument in main method. When we are using "String... s" then there is no need to pass any string array as an argument. 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/////////
除了这两种风格之外,值得一提的是,以下也是自 Java 5 以来
main()
方法的有效签名: com/javase/specs/jls/se8/html/jls-12.html#jls-12.1.4" rel="nofollow noreferrer">Java 语言规范:
省略号
...
是 var-args 的语法,可用于将任意数量的参数传递给方法。更多信息请参见此处。Apart from these two styles, it is worth mentioning that the following is also a valid signature for the
main()
method since Java 5:From the Java Language specification:
The ellipsis
...
is the syntax for var-args, which can be used to pass an arbitrary number of arguments to a method. More on it here.String args[] 和 String[] args 是相同的。从某种意义上说,它们做同样的事情,创建一个名为 args 的字符串数组。
但为了避免混淆并强制与您可能遇到的所有其他 Java 代码兼容,我建议在声明数组时使用语法 (String[] args)。
您可能会遇到的一个困惑是,当您尝试在一行中声明多个 String 数组时,
String[]Fruits, Vegetables
将与Stringfruits[], Vegetables
不同>前者创建两个字符串数组fruits和vegetables,而后者创建一个fruits字符串数组和一个vegetables字符串变量
String args[] and String[] args are identical. In the sense that they do the same thing, Creating a string array called 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.
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 asString 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
有一个区别:
例如
如果你写 int[] arr; - 这里 arr 是对整数数组的引用
而对于 int arr[]; - arr 是一个原始 int 类型数组
there is a difference:
For example
if you write int[] arr; - here arr is the reference to the integer array
whereas in case of int arr[]; - arr is a primitive int type array