I have recently been thinking about the difference between the two ways of defining an array:
int[] array
int array[]
Is there a difference?
I have recently been thinking about the difference between the two ways of defining an array:
int[] array
int array[]
Is there a difference?
From section 10.2 of the Java Language Specification:
Personally almost all the Java code I've ever seen uses the first form, which makes more sense by keeping all the type information about the variable in one place. I wish the second form were disallowed, to be honest... but such is life...
Fortunately I don't think I've ever seen this (valid) code:
The most preferred option is
int[] a
- becauseint[]
is the type, anda
is the name. (your 2nd option is the same as this, with misplaced space)Functionally there is no difference between them.
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.when declaring a single array reference, there is not much difference between them. so the following two declarations are same.
when declaring multiple array references, we can find difference between them. the following two statements mean same. in fact, it is up to the programmer which one is follow. but the standard java notation is recommended.
As already stated, there's no much difference (if you declare only one variable per line).
Note that SonarQube treats your second case as a minor code smell:
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):