gpt4 book ai didi

java - "Object[] x"和 "Object x[]"之间有什么区别吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:27:08 25 4
gpt4 key购买 nike

我正在用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com