7

Is there any difference between between these two array declaration syntax in java?

int[] variableName; 

and

int variableName[];

Which one is preferred?

Óscar López
  • 232,561
  • 37
  • 312
  • 386
Adi
  • 134
  • 11
  • 3
    The first one is preferred for consistency. Also type information is not scattered as in the latter. It's easy to miss the [] if it's not attached to the base type. – egaga Aug 25 '12 at 13:51
  • There is no functional difference. The only difference could be if one is better understood by a coder than the other. – Hovercraft Full Of Eels Aug 25 '12 at 14:01

7 Answers7

16

Although both syntaxes are equivalent, the int[] variableName; syntax is preferred. The int variableName[]; syntax is allowed just for making comfortable all the C/C++ programmers transitioning to Java.

One could argue that int[] x clearly states that integer array is the type of x, whereas in int x[] the actual type is split in two parts, one before and the other after the x variable, reading like integer is the type of the variable x which is an array making the declaration less readable and potentially confusing to a newcomer.

The readability problem is exacerbated if the array has more dimensions, for example all of these declarations are equivalent and valid:

int x[][][];
int[] x[][];
int[][] x[];
int[][][] x;  // this one is the easiest to read!

Also notice that the same considerations apply to all these valid, equivalent method declarations (the return type is the same in all cases) - but once again the last one is simpler to read:

int m() [][][] {return null;}
int[] m() [][] {return null;}
int[][] m() [] {return null;}
int[][][]  m() {return null;}  // this one is the easiest to read!
Óscar López
  • 232,561
  • 37
  • 312
  • 386
5

The only case where the postfix syntax is essential is a very non-essential use case:

class ArrayShowcase {
  private int i, ints[], intints[][];
}

Do we need such declarations? Well, we could live without them, I think. I've never once needed it in the many hundreds of thousands of lines of code I've written in my professional carreer.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

As already mentioned they are both the same and the first one is preferred. I'd like to add that it's also essential to be consistent with the respective usage. If you change a code base that uses one of the two, stay with the same convention.

halex
  • 16,253
  • 5
  • 58
  • 67
1

They are semantically identical. The int variablename[] syntax was only added to help C programmers get used to java.

int[] variable name is much preferable, and less confusing.

See Here

Community
  • 1
  • 1
heretolearn
  • 6,387
  • 4
  • 30
  • 53
0

They both mean the same thing, so there is no difference syntactically. However, I have seen developers using either one or the other.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

I would use int[] var as it is more explicit. It tells teh reader that it is an array of int. If you have the [] next to the variable name the user has to scan accross the line to see the type of this array. Personally I would (and most java programmers I know) use int[]

RNJ
  • 15,272
  • 18
  • 86
  • 131
0

The difference is conceptual, for human reading. They define the same thing, but

int[] variableName keeps the type of variableName consistent in contrast to

int variableName[] where, there is stated that the array variable is of type int.

Bakudan
  • 19,134
  • 9
  • 53
  • 73