“Object[] x”和“Object[] x”之间有什么区别吗?和“对象x[]”?

发布于 2024-08-20 09:53:02 字数 325 浏览 5 评论 0原文

我正在更新 Java 中的遗留代码库,发现了这样一行:

Object arg[] = { new Integer(20), new Integer(22) };

该行引起了我的注意,因为我习惯了这种代码:

Object[] arg = { new Integer(20), new Integer(22) };

数组的内容在这里并不重要。我很好奇变量名旁边的括号与类名旁边的括号。我在 Eclipse(使用 Java 5)中尝试过,这两行对于编译器都有效。

这些声明之间有什么区别吗?

I was updating a legacy code base in Java and I found a line like this:

Object arg[] = { new Integer(20), new Integer(22) };

That line catched my attention because I am used to this kind of code:

Object[] arg = { new Integer(20), new Integer(22) };

The content of the array isn't important here. I'm curious about the brackets next to the variable name versus the brackets next to the class name. I tried in Eclipse (with Java 5) and both lines are valid for the compiler.

Is there any difference between those declarations?

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

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

发布评论

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

评论(5

单调的奢华 2024-08-27 09:53:02

两者都是合法且有效的。但建议将 [] 放在数组名称之前。

来自 Javadocs

您还可以将方括号放在数组名称后面:

float anArrayOfFloats[]; // this form is discouraged

然而,惯例不鼓励这种形式;括号标识数组类型,并且应与类型名称一起出现。

Both are legal and both work. But placing [] before the array's name is recommended.

From Javadocs:

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.

水晶透心 2024-08-27 09:53:02

不,他们都工作。但请注意:

float anArrayOfFloats[], aSecondVariable;

将声明一个浮点数数组和一个浮点数,而:

float[] anArrayOfFloats, aSecondVariable;

将声明两个 浮点数数组。

No, they both work. But watch out:

float anArrayOfFloats[], aSecondVariable;

will declare one array of floats and one float, while:

float[] anArrayOfFloats, aSecondVariable;

will declare two arrays of floats.

机场等船 2024-08-27 09:53:02

没有什么区别。两者都是合法的。

您可以阅读Java语言规范 http://java .sun.com/docs/books/jls/second_edition/html/arrays.doc.html

[] 可能作为类型的一部分出现
在声明的开头,
或作为声明符的一部分
特定变量或两者,如
这个例子:

byte[] 行向量、列向量、矩阵[];

There is no difference. Both are legal.

You can read in Java Language Specification http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

The [] may appear as part of the type
at the beginning of the declaration,
or as part of the declarator for a
particular variable, or both, as in
this example:

byte[] rowvector, colvector, matrix[];
夕色琉璃 2024-08-27 09:53:02

编写 Integer[] ints 而不是 Integer ints[] 的另一个好理由是继承关系:Integer[]的子类型>Number[]Object[] 的子类型。

换句话说,您可以将 Integers 放入 Object 数组中,因此您可以将 [] 视为对象类型定义的一部分 - - 这就是为什么让它接近类型而不是对象名称是有意义的。

Another good reason to write Integer[] ints instead of Integer ints[] is because of inheritance relations: Integer[] is subtype of Number[] is subtype of Object[].

In other words, you can put Integers in an Object array, so you can think of the [] as part of the object's type definition -- which is why it makes sense to have it close to the type instead of the object name.

盛夏已如深秋| 2024-08-27 09:53:02

简短回答:不。

Short answer: No.

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