Java: int [] array vs int array []

Possible duplicate:
The difference between int [] and int array []

Is there any difference between

int[] array = new int[10]; 

and

 int array[] = new int[10]; 

?

Both work, and the result is exactly the same. Which one is faster or better? Is there a leadership style that one recommends?

+51
java performance arrays definition cpu-speed
Jan 28 '13 at 10:13
source share
8 answers

Both are equivalent. Take a look at the following:

 int[] array; // is equivalent to int array[]; 
 int var, array[]; // is equivalent to int var; int[] array; 
 int[] array1, array2[]; // is equivalent to int[] array1; int[][] array2; 
 public static int[] getArray() { // .. } // is equivalent to public static int getArray()[] { // .. } 
+97
Jan 28 '13 at 10:19
source share

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

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

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

Here are some examples of array variable declarations 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", }; 

[] It may appear as part of the type at the beginning of the declaration, or as part of the declaration for a specific variable, or both , as in this example:

 byte[ ] rowvector, colvector, matrix[ ]; 

This declaration is equivalent to:

 byte rowvector[ ], colvector[ ], matrix[ ][ ]; 
+8
Jan 28 '13 at 10:18
source share

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

 int[] array = new int[10]; 

FROM JLS :

[] can be displayed as part of the type at the beginning of the declaration, or as part of the declarator for a specific variable, or both.

+7
Jan 28 '13 at 10:15
source share

Both are the same. Usually I use int[] array = new int[10]; due to better (continuous) readability of the int[] .

+4
Jan 28 '13 at 10:16
source share

No, there is no difference. But I prefer to use int[] array as it is more readable.

+2
Jan 28 '13 at 10:16
source share

There is no difference between the two ads, and both have the same performance.

+2
Jan 28 '13 at 10:59
source share

There is practically no difference.

0
Jan 28 '13 at 10:15
source share

In both examples, you assign the reference variable new int[10] .

Assigning a reference variable in any case will be the same in performance.

 int[] array = new int[10]; 

The designations above are considered best practice for readability.

Greetings

0
Jan 28 '13 at 10:19
source share



All Articles