“Object[] x"和“Object[] x"之间有什么区别吗?和“对象 x[]"? [英] Is there any difference between "Object[] x" and "Object x[]"?
问题描述
我正在用 Java 更新遗留代码库,我发现这样一行:
Object arg[] = { new Integer(20), new Integer(22) };
那行引起了我的注意,因为我已经习惯了这种代码:
Object[] arg = { new Integer(20), new Integer(22) };
数组的内容在这里并不重要.我很好奇变量名旁边的括号与类名旁边的括号.我在 Eclipse(使用 Java 5)中尝试过,这两行都对编译器有效.
这些声明之间有什么区别吗?
两者都是合法的,并且都有效.但建议将 [] 放在数组名称之前.
来自 Javadocs:
<块引用>您也可以将方括号放在数组名称之后:
float anArrayOfFloats[];//这种形式是不鼓励的
<块引用>
然而,惯例不鼓励这种形式;括号标识数组类型,并应与类型名称一起出现.
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?
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.
这篇关于“Object[] x"和“Object[] x"之间有什么区别吗?和“对象 x[]"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!