我最近一直在思考定义数组的两种方式之间的区别:

int[]数组 int[]数组

有区别吗?


当前回答

不,这些是一样的。然而

byte[] rowvector, colvector, matrix[];

等价于:

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

摘自Java规范。这意味着

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

是不同的。我不推荐这两种声明。最容易读的(可能)是:

int[] a;
int[] b;

其他回答

Java语言规范说:

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[][];

因此,它们将产生完全相同的字节代码。

没有区别,但是Sun建议把它放在这里解释的类型旁边

两者的意思是一样的。然而,这些变量的存在也允许:

int[] a, b[];

也就是:

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

然而,这是一种可怕的编码风格,不应该这样做。

是的,有区别。

Int [] a = new Int [100];// 'a'本身不是数组,数组存储为地址 在内存的其他地方,'a'只保存该地址

Int b[] = new Int [100];//当创建像cleary这样的数组时,显示'b'是一个数组,并且它 整数类型。

它们完全相等。Int[]数组是首选样式。int array[]只是作为等效的c兼容样式提供。