Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between two array declarations in Java? [duplicate]

Tags:

java

arrays

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

like image 406
Alper Avatar asked Sep 01 '11 23:09

Alper


People also ask

What is the difference between int [] A and int a [] in Java?

Difference between “int[] a” and “int a[]” for 1-D Arrays in Java. For 1-D Array, in Java, there is no difference and any of the mentioned syntaxes can be used to declare a 1-D array.

What are the different types of declaring an array in Java?

There are two ways you can declare and initialize an array in Java. The first is with the new keyword, where you have to initialize the values one by one. The second is by putting the values in curly braces.

How do you compare two arrays equal in Java?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

How do you compare two elements in an array?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.


2 Answers

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.

like image 59
Oscar Gomez Avatar answered Sep 27 '22 22:09

Oscar Gomez


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
like image 45
Yanick Rochon Avatar answered Sep 27 '22 22:09

Yanick Rochon