‘clas [] arrayname’ 与 ‘class arrayname[]’ 之间的区别是什么?

huangapple go评论68阅读模式
英文:

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;
  1. 你声明了一个名为 array 的变量,其类型是 String[](字符串数组);
  2. 在这段代码中,你可以内联地声明其他具有相同 String[] 类型的变量(String[] array1, array2;);

使用:

String array[];
  1. 你声明了一个变量,该变量将引用数组对象,该对象的元素必须是 java.lang.String 类型;
  2. 你可以像这样使用 String array[], name, surname;,尽管这并不是一个很好的编码习惯,但 Java 语义并不阻止这样做。
英文:

They are almost same, with a little semantical catch.

By:

String[] array;
  1. You declare the variable named array which is of the type of String[] (String array);
  2. In this code, you can declare other variables in-line, with the same String[] type (String[] array1, array2;);

By:

String array[];
  1. You declare a variable which will be referring to the array object, elements of which, must be type of java.lang.String;
  2. 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.

huangapple
  • 本文由 发表于 2020年9月25日 20:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64064074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定