10

In my book they've been switching the way the declare arrays between the following two methods:

    int array1[] = {1, 2, 3};
    int[] array2 = {1, 2, 3};

I was wondering what the difference was between the positioning of the two brackets, and why is it that when I put the brackets after the name (such as in array 1), why I must either initialize it to a set of values or a new array, but in array2, I can simply say "int[] array2;" and then use it later...?

Alper
  • 1
  • 12
  • 39
  • 78
  • Nothing. The two are equivalent (when initialized). The second is preferred, but the first is there for old C-heads. – Hot Licks Sep 01 '11 at 23:50
  • They are equivalent. Also, `int[][] array1`, `int[] array1[]` and `int array1[][]` are equivalent. – Eng.Fouad Sep 02 '11 at 00:29

3 Answers3

7

They are identical except that as you mention you have to initialize it if you put the brackets after the name. One advantage of declaring them before the name is multiple array initialization like this:

int [] myArray1, myArray2;

int myArray1[], myArray2[];

The Java way according to the documentation is to put the brackets before the array name.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • Yes, but why? Is it so difficult to figure out that the first example is supposed to be an array if it's not followed by an initialization? – StriplingWarrior Sep 01 '11 at 23:51
  • So, would you say the second array declaration is better, just because it offers more flexibility? – Alper Sep 01 '11 at 23:52
  • @Jacob Yes it does offer more flexibility, and according to the docs is the preferred way, there is also a difference when you are initializing multiple arrays. – Oscar Gomez Sep 01 '11 at 23:56
  • 1
    @StriplingWarrior I am not sure on that one, but apparently the other form is just there to be C friendly for other programmers learning Java. – Oscar Gomez Sep 01 '11 at 23:58
  • 3
    `int[]` denotes a type. Using that notation the bits of the type don't get separated as they do with the C-style notation. – Hot Licks Sep 02 '11 at 00:01
1

There is no difference between them as they both declare a variable of type "array of ints". There is no "Java way" (but a preferred way), even if the documentation, arrays are declared with brackets before the variable name :

int[] array1;

Note : Pay attention that "declaration" is not "initialisation" (or instanciation)

int[] array1;   // declares an array of int named "array1"
                // at this point, "array1" is NOT an array, but null
                // Thus we "declare" a variable that will hold some 
                // data of type int[].

array1 = new int[] {1, 2, 3};   // legacy initialization; set an array of int
                                // with 3 values to "array1". Thus, we "initialize"
                                // the variable with some data of type int[]

Therefore,

int [] array1;
int array2[];

both declares two variables of type int[]; however, they are just declarations of data types, and not arrays yet. And like Oscar Gomez said, the difference now is that the second "way" requires you to specify that the variable is of type array, and not only an int.

int i[], j;   // i is a data type array of int, while j is only an int
int [] k, l;  // both k and l are of the same type: array of int
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
0

An array is created by an array creation expression or an array initializer. The former applies to array2 (although in this case, an array initializer was provided), whereas the latter applies to array1.

For more information, see:

mre
  • 43,520
  • 33
  • 120
  • 170