up vote 12 down vote favorite

Hello. I have recently been thinking of the difference between the two ways of defining an array:

  1. int[] array
  2. int array[]

Is there a difference? I havent found anything in my Java book explaining me the difference and googling the question isnt bringing me any closer... I just dont know what to google after.

Thanks

link|flag

19 Answers

up vote 28 down vote accepted

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.

link|flag
5  
The [] is part of the TYPE, not of the NAME. For me, that's the biggest difference. – André Neves Sep 24 '08 at 22:50
up vote 25 down vote

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.
link|flag
up vote 20 down vote

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.

link|flag
3  
Writing that in real code would an instant sacking offence :) – skaffman Sep 24 '08 at 19:33
up vote 11 down vote

There isn't any difference between the two; both declare an array of ints. 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.

link|flag
up vote 10 down vote

There is no real difference; however,

double[] items = new double[10];

is preferred as it clearly indicates that the type is an array.

link|flag
double items[] is really there for the C programmers – Steve Kuo Feb 25 '09 at 20:03
@Steve, That must be why I keep doing it that way. :-) – Paul Tomblin Feb 25 '09 at 20:04
Let's see: public static void main( String [] args ) .... I use the later. – OscarRyz Feb 25 '09 at 21:23
A counterargument can be made that double items[] clearly indicates the type and later that items just so happens to be an array - it all depends on what you're comfortable with. – MetroidFan2002 Mar 1 '09 at 6:02
up vote 8 down vote

No difference.

Quoting from Sun:

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example: byte[] rowvector, colvector, matrix[];

This declaration is equivalent to: byte rowvector[], colvector[], matrix[][];

link|flag
up vote 4 down vote

The two commands are the same thing.

You can use the syntax to declare multiple objects:

int[] arrayOne, arrayTwo; //both arrays

int arrayOne[], intOne; //one array one int

see: http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html

link|flag
up vote 3 down vote

There is no difference, but Sun recommends putting it next to the type as explained here

link|flag
up vote 1 down vote

Yep, exactly the same. Personally, I prefer

int[] integers; 

because it makes it immediately obvious to anyone reading your code that integers is an array of int's, as opposed to

int integers[];

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.

link|flag
up vote 0 down vote

In Java, these are simply different syntactic methods of saying the same thing.

link|flag
up vote 0 down vote

They're the same. One is more readable (to some) than the other.

link|flag
up vote 0 down vote

They are completely equivalent. int [] array is the preferred style. int array[] is just provided as an equivalent, C-compatible style.

link|flag
up vote 0 down vote

No difference.

link|flag
up vote 0 down vote

Wow that went fast. Thanks to all!

link|flag
If it went so well, then pick an answer! ;-) – Forgotten Semicolon Sep 24 '08 at 19:15
1  
Hehe, I'm am new to this wonderful system. – mslot Sep 24 '08 at 19:17
up vote 0 down vote

I didn't even know that int array[] was possible. Please don't do it. :)

link|flag
up vote 0 down vote

there isn't really any difference between the two, most of it is purely personal preference though. I personally prefer using type[] name; as it clearly shows the type, and makes it a lot easier when scanning through your own code (as long as it is indented properly - and if you don't know how use some software that does it for you i.e. notepad++ - good for many languages, though just a text editor, and then learn from them - you can't program unless you indent!!!!!)

link|flag
up vote 0 down vote

Both are ok. I suggest to pick one and stick with it. (I do the second one)

link|flag
up vote 0 down vote

They are the same, but there is an important difference between these statements:

// 1.
int regular, array[];
// 2.
int[] regular, array;

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.

link|flag
Thanx all! I'll go with the second one and stick with it. – Espen Oct 24 at 10:16
up vote 0 down vote

While the int integers[] solution roots in the C language (and can be thus considered the "normal" approach), many people find int[] integers more logical as it disallows to create variables of different types (i.e. an int and an array) in one declaration (as opposed to the C-style declaration).

link|flag

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.