Is there a difference between
int[] array = new int[10];
and
int array[] = new int[10];
?
Both do work, and the result is exactly the same. Which one is quicker or better? Is there a style guide which recommends one?
What is the difference between int[] a and int a[] in Java? There is no difference in these two types of array declaration. There is no such difference in between these two types of array declaration. It's just what you prefer to use, both are integer type arrays.
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. The [] is part of the TYPE, not of the NAME.
Since int[] is a class, it can be used to declare variables. For example, int[] list; creates a variable named list of type int[]. This variable is capable of referring to an array of ints, but initially its value is null (if it is a member variable in a class) or undefined (if it is a local variable in a method).
int - stores integers (whole numbers), without decimals, such as 123 or -123. float - stores floating point numbers, with decimals, such as 19.99 or -19.99.
Both are equivalent. Take a look at the following:
int[] array; // is equivalent to int array[];
int var, array[]; // is equivalent to int var; int[] array;
int[] array1, array2[]; // is equivalent to int[] array1; int[][] array2;
public static int[] getArray() { // .. } // is equivalent to public static int getArray()[] { // .. }
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