tags:

views:

2614

answers:

19

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

+28  A: 

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.

skaffman
The [] is part of the TYPE, not of the NAME. For me, that's the biggest difference.
André Neves
A: 

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

Zach Lute
A: 

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

basszero
A: 

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

Derek Park
A: 

No difference.

Iulian Şerbănoiu
+3  A: 

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

André
A: 

Wow that went fast. Thanks to all!

mslot
If it went so well, then pick an answer! ;-)
Forgotten Semicolon
Hehe, I'm am new to this wonderful system.
mslot
+4  A: 

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

George Strother
+20  A: 

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.

Adam Rosenfield
Writing that in real code would an instant sacking offence :)
skaffman
A: 

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

skiphoppy
+11  A: 

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.

Pourquoi Litytestdata
+8  A: 

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[][];

Yuval A
+25  A: 

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.
TofuBeer
+10  A: 

There is no real difference; however,

double[] items = new double[10];

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

Aaron Maenpaa
double items[] is really there for the C programmers
Steve Kuo
@Steve, That must be why I keep doing it that way. :-)
Paul Tomblin
Let's see: public static void main( String [] args ) .... I use the later.
OscarRyz
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
A: 

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!!!!!)

lavamunky
+1  A: 

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.

David Watson
A: 

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

Albert
A: 

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.

Patrick
Thanx all! I'll go with the second one and stick with it.
Espen
A: 

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).

Kos