Consider this code:
class arraytest {
public static void main(String args[]) {
int[] a = null , b[] = null;
b = a;
System.out.println( b );
}
}
The line
b = a;
is flagged by the compiler saying:
Incompatible types, found int[], required int [][]
Why is b considered two dimensional? I realize the "shortcut" declaration
int[] a = null , b[] = null;
is to blame, but why does it make the array two dimensional when only one set of brackets have been written? I find this syntax unclear and obfuscating.
Take this example:
int a, b;
Then both a and b are ints, right? So now take this:
int[] a, b;
The both a and b are int arrays. So by adding another set of brackets:
int[] a, b[];
you have added another set of brackets to b.
Either of these would be fine:
int[] a = null , b = null;
int a[] = null , b[] = null;
or as you say, simply putting them on separate lines would work too (and be much easier to read).
int[] a = null , b[] =null;
.... it's equal to :
int[] a = null;
int[][]b = null;
You have to do :
int[] a = null , b =null;
In addition to the other answers, Java does not have multi-dimensional arrays. Java has arrays of any type. This "any type" may itself be an array. Multi-dimensional arrays are distinctly different, although subtly.
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