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

int[]数组 int[]数组

有区别吗?


当前回答

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

其他回答

不,这些是一样的。然而

byte[] rowvector, colvector, matrix[];

等价于:

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

摘自Java规范。这意味着

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

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

int[] a;
int[] b;

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

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

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

这两个命令是一样的。

你可以使用语法来声明多个对象:

int[] arrayOne, arrayTwo; //both arrays

int arrayOne[], intOne; //one array one int 

参见:http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

这两种类型的声明在功能上没有区别。两者都声明数组为int。

但是int[] a将类型信息保存在一起,更详细,所以我更喜欢它。