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 semantically identical. The
|
|||||||||||||||||||||
|
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 |
|||||
|
There is no difference. I prefer the EDIT: Oh wait there is a difference (I forgot because I never declare more than one variable at a time):
|
||||
|
No, these are the same. However
is equivalent to:
Taken from Java Specification. That means that
are different. I would not recommend either of these multiple declarations. Easiest to read would (probably) be:
|
||||
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 two commands are the same thing. You can use the syntax to declare multiple objects:
see: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html |
||||
|
No difference. Quoting from Sun:
|
|||
|
There is no real difference; however,
is preferred as it clearly indicates that the type is an array. |
|||||||||||||||||
|
There isn't any difference between the two; both declare an array of |
|||
|
There is no difference, but Sun recommends putting it next to the type as explained here |
|||
|
It is an alternative form, which was borrowed from As a curiosity, there are three ways to define a valid
|
|||||||||||||
|
Both are equally valid. The
Note the last paragraph. I recommend reading the official Sun/Oracle tutorials rather than some 3rd party ones. You would otherwise risk end up in learning bad practices. |
|||
|
The most preferred option is Functionally there is no difference between them. |
|||
|
In Java, these are simply different syntactic methods of saying the same thing. |
|||
|
The Java Language Specification says:
Thus they will result in exactly the same byte code. |
|||
|
There is no difference in functionality between both styles of declaration. Both declare array of int. But |
||||
|
They are the same, but there is an important difference between these statements:
in 1. regular is just an int, as opposed to 2. where both regular and array are arrays of int's. The second statement you have is therefore preferred, since it is more clear. The first form is also discouraged according to this tutorial on Oracle. |
|||||
|
They're the same. One is more readable (to some) than the other. |
|||
|
They are completely equivalent. |
|||
|
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. |
|||||||||
|
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. |
|||
|
Both are ok. I suggest to pick one and stick with it. (I do the second one) |
|||
|
While the |
|||
|