英文:
difference between 'class[] arrayname' and 'class arrayname[]'
问题
我不理解以下两种写法之间的区别:
String[] arrayName = {/*这里放一些数据*/};
和
String arrayName[] = {/*这里放一些数据*/};
在类型后面放方括号(String[]
)和在数组名后面放方括号(String arrayName[]
)之间是否有区别?如果有,是什么?
英文:
I don't understand the difference between
String[] arrayName = {/*some data here*/};
and
String arrayName[] = {/*some data here*/};
Is there any difference between placing square brackets after the type (String[]
) and after the array name (String arrayName[]
)?<br/>
If so, what?
答案1
得分: 4
两种声明将会有相同的结果。区别只在于风格。话虽如此,大多数 Java 风格指南会建议将方括号放在类型名称上,而不是变量名称上,即:
String[] arrayName = {/*这里放一些数据*/};
英文:
Both declarations will have the same result. The difference is only stylistic. Having said that, most Java style guides would recommend having the square brackets on the type name, not the variable name, i.e.:
String[] arrayName = {/*some data here*/};
答案2
得分: 0
他们几乎是相同的,只有一点点语义上的区别。
使用:
String[] array;
- 你声明了一个名为
array
的变量,其类型是 String[](字符串数组); - 在这段代码中,你可以内联地声明其他具有相同
String[]
类型的变量(String[] array1, array2;
);
使用:
String array[];
- 你声明了一个变量,该变量将引用数组对象,该对象的元素必须是
java.lang.String
类型; - 你可以像这样使用
String array[], name, surname;
,尽管这并不是一个很好的编码习惯,但 Java 语义并不阻止这样做。
英文:
They are almost same, with a little semantical catch.
By:
String[] array;
- You declare the variable named
array
which is of the type of String[] (String array); - In this code, you can declare other variables in-line, with the same
String[]
type (String[] array1, array2;
);
By:
String array[];
- You declare a variable which will be referring to the array object, elements of which, must be type of
java.lang.String
; - You can have something like
String array[], name, surname;
which is not really a decent code to write, but Java semantics do not block this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论