Difference between int[] array and int array[]
I have recently been thinking about the difference between the two ways of defining an array:
int[] array
int array[]
Is there a difference?
Solution 1:
They are semantically identical. The int array[]
syntax was only added to help C programmers get used to java.
int[] array
is much preferable, and less confusing.
Solution 2:
There is one slight difference, if you happen to declare more than one variable in the same declaration:
int[] a, b; // Both a and b are arrays of type int
int c[], d; // WARNING: c is an array, but d is just a regular int
Note that this is bad coding style, although the compiler will almost certainly catch your error the moment you try to use d
.
Solution 3:
There is no difference.
I prefer the type[] name
format at is is clear that the variable is an array (less looking around to find out what it is).
EDIT:
Oh wait there is a difference (I forgot because I never declare more than one variable at a time):
int[] foo, bar; // both are arrays
int foo[], bar; // foo is an array, bar is an int.