Difference between int[] array and int array[]

2018-12-31 01:53发布

I have recently been thinking about the difference between the two ways of defining an array:

  1. int[] array
  2. int array[]

Is there a difference?

标签: java arrays
25条回答
与风俱净
2楼-- · 2018-12-31 02:32

There is no difference, but Sun recommends putting it next to the type as explained here

查看更多
几人难应
3楼-- · 2018-12-31 02:34

There is no real difference; however,

double[] items = new double[10];

is preferred as it clearly indicates that the type is an array.

查看更多
回忆,回不去的记忆
4楼-- · 2018-12-31 02:36

They're the same. One is more readable (to some) than the other.

查看更多
柔情千种
5楼-- · 2018-12-31 02:36

While the int integers[] solution roots in the C language (and can be thus considered the "normal" approach), many people find int[] integers more logical as it disallows to create variables of different types (i.e. an int and an array) in one declaration (as opposed to the C-style declaration).

查看更多
无色无味的生活
6楼-- · 2018-12-31 02:37

No, these are the same. However

byte[] rowvector, colvector, matrix[];

is equivalent to:

byte rowvector[], colvector[], matrix[][];

Taken from Java Specification. That means that

int a[],b;
int[] a,b;

are different. I would not recommend either of these multiple declarations. Easiest to read would (probably) be:

int[] a;
int[] b;
查看更多
千与千寻千般痛.
7楼-- · 2018-12-31 02:37

No difference.

Quoting from Sun:

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example: byte[] rowvector, colvector, matrix[];

This declaration is equivalent to: byte rowvector[], colvector[], matrix[][];

查看更多
登录 后发表回答