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?
They are completely equivalent.
int [] array
is the preferred style.int array[]
is just provided as an equivalent, C-compatible style.There is one slight difference, if you happen to declare more than one variable in the same declaration:
Note that this is bad coding style, although the compiler will almost certainly catch your error the moment you try to use
d
.It is an alternative form, which was borrowed from
C
, upon which java is based.As a curiosity, there are three ways to define a valid
main
method in java:public static void main(String[] args)
public static void main(String args[])
public static void main(String... args)
Yep, exactly the same. Personally, I prefer
because it makes it immediately obvious to anyone reading your code that integers is an array of int's, as opposed to
which doesn't make it all that obvious, particularly if you have multiple declarations in one line. But again, they are equivalent, so it comes down to personal preference.
Check out this page on arrays in Java for more in depth examples.
There isn't any difference between the two; both declare an array of
int
s. However, the former is preferred since it keeps the type information all in one place. The latter is only really supported for the benefit of C/C++ programmers moving to Java.Both have the same meaning. However, the existence of these variants also allows this:
which is the same as:
However, this is horrible coding style and should never be done.