Java: int[] array vs int array[] [duplicate] Java: int[] array vs int array[] [duplicate] arrays arrays

Java: int[] array vs int array[] [duplicate]


Both are equivalent. Take a look at the following:

int[] array;// is equivalent toint array[];
int var, array[];// is equivalent toint var;int[] array;
int[] array1, array2[];// is equivalent toint[] array1;int[][] array2;
public static int[] getArray(){    // ..}// is equivalent topublic static int getArray()[]{    // ..}


From JLS http://docs.oracle.com/javase/specs/jls/se5.0/html/arrays.html#10.2

Here are examples of declarations of array variables that do not create arrays:

int[ ] ai;          // array of intshort[ ][ ] as;         // array of array of shortObject[ ]   ao,     // array of Object        otherAo;    // array of ObjectCollection<?>[ ] ca;        // array of Collection of unknown typeshort       s,      // scalar short         aas[ ][ ];  // array of array of short

Here are some examples of declarations of array variables that create array objects:

Exception ae[ ] = new Exception[3]; Object aao[ ][ ] = new Exception[2][3];int[ ] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 };char ac[ ] = { 'n', 'o', 't', ' ', 'a', ' ',                 'S', 't', 'r', 'i', 'n', 'g' }; String[ ] aas = { "array", "of", "String", };

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


They are both basically same, there is no difference in performance of any sort, the recommended one however is the first case as it is more readable.

int[] array = new int[10];

FROM JLS:

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.