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

发布于 2024-11-06 23:19:39 字数 177 浏览 0 评论 0 原文

之间有区别吗:

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

public void 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

淡看悲欢离合 2024-11-13 23:19:39

从语义上来说,它们是相同的。但是,我建议在声明数组时使用后一种语法 (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.

情话已封尘 2024-11-13 23:19:39

没有什么区别,但在类型后面加上括号 (String[]) 是 Java 中更常见的做法。

There's no difference, but putting the brackets after the type (String[]) is the more common practice in Java.

你没皮卡萌 2024-11-13 23:19:39

两者完全一样。请参阅Java语言规范(JLS)来查看java中使用的语法。

String[] argsString args[] 将创建一个没有大小和名称 args 的数组(在内存中保留一个位置)。

让我们考虑一下:

String [] x;

[] 放在名称后面可以避免像这样的微妙问题:-

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

Both of them are absolutely the same. Please refer to Java Language Specification (JLS) to see the syntax used in java.

String[] args or String args[] will create an array (reserves a place in memory) with no size and name 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
只怪假的太真实 2024-11-13 23:19:39

虽然两者都用于创建 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.

月棠 2024-11-13 23:19:39

它们只是两种写作风格

No

They are just two style of writting

陪你到最终 2024-11-13 23:19:39

方法签名相同,因此没有区别。

它是一个公共方法,它不返回任何内容,方法名称是“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.

过气美图社 2024-11-13 23:19:39

没有。两者没有任何区别。

请参阅 声明变量以引用数组部分>本文档。从那个文档

浮点 anArrayOfFloats[]; // 这个表格
气馁

所以只使用第二种风格。

Nope. There is no difference b/w the two.

See Declaring a Variable to Refer to an Array section in this doc. From that doc

float anArrayOfFloats[]; // this form
is discouraged

so just use the second style.

吹梦到西洲 2024-11-13 23:19:39

它们之间的区别在于,当我们尝试使用静态语法从一个类手动调用 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/////////

握住我的手 2024-11-13 23:19:39

除了这两种风格之外,值得一提的是,以下也是自 Java 5 以来 main() 方法的有效签名

public static void main(String... args)

: com/javase/specs/jls/se8/html/jls-12.html#jls-12.1.4" rel="nofollow noreferrer">Java 语言规范:

main 方法必须声明为 public、static 且 void。它必须指定一个声明类型为 String 数组的形参(第 8.4.1 节)。

省略号 ... 是 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:

public static void main(String... args)

From the Java Language specification:

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.

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.

悍妇囚夫 2024-11-13 23:19:39

String args[] 和 String[] args 是相同的。从某种意义上说,它们做同样的事情,创建一个名为 args 的字符串数组。

但为了避免混淆并强制与您可能遇到的所有其他 Java 代码兼容,我建议在声明数组时使用语法 (String[] args)。

您可能会遇到的一个困惑是,当您尝试在一行中声明多个 String 数组时, String[]Fruits, Vegetables 将与 Stringfruits[], Vegetables 不同>

前者创建两个字符串数组fruitsvegetables,而后者创建一个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 as 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

萌化 2024-11-13 23:19:39

有一个区别:
例如
如果你写 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文