58

Is there a difference between

int[] array = new int[10];

and

int array[] = new int[10];

?

Both do work, and the result is exactly the same. Which one is quicker or better? Is there a style guide which recommends one?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
xuma202
  • 1,074
  • 1
  • 10
  • 22
  • 2
    Official recommendation: the second notation is "Yechh": http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.2-210 – assylias Jan 28 '13 at 10:21
  • @assylias I think the yechhh referred to doing int[] a[], instead of int[][] a. It's related, but not quite the same a what OP was asking about. – Joshua Taylor Nov 14 '17 at 16:43

8 Answers8

107

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()[]
{
    // ..
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
9

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 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 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[ ][ ];
Shashi
  • 12,487
  • 17
  • 65
  • 111
  • The latest version of the JLS is clearer on the subject: http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.2-210 – assylias Jan 28 '13 at 10:22
8

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.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
4

Both are the same. I usually use int[] array = new int[10];, because of better (contiguous) readability of the type int[].

Sam
  • 7,778
  • 1
  • 23
  • 49
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Harinder
  • 11,776
  • 16
  • 70
  • 126
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sumit sharma
  • 1,067
  • 8
  • 24
0

There is virtually no difference.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

In both examples, you are assigning a new int[10] to a reference variable.

Assigning to a reference variable either way will be equal in performance.

int[] array = new int[10];

The notation above is considered best practice for readability.

Cheers

Dennis Jaamann
  • 3,547
  • 2
  • 23
  • 42