我正在用 Java 更新遗留代码库,我发现了这样一行:
Object arg[] = { new Integer(20), new Integer(22) };
那一行引起了我的注意,因为我已经习惯了这种代码:
Object[] arg = { new Integer(20), new Integer(22) };
数组的内容在这里并不重要。我很好奇变量名旁边的括号与类名旁边的括号。我在 Eclipse(使用 Java 5)中尝试过,这两行都对编译器有效。
这些声明之间有什么区别吗?
最佳答案
两者都是合法的,而且都有效。但建议将 [] 放在数组名称之前。
来自 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.
关于java - "Object[] x"和 "Object x[]"之间有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2229602/