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...?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With