What is this array?

arrays, java

Solution

All those 3 declarations have the same meaning in java :

Boolean  [][] ba ;
Boolean  [] ba [];
Boolean  ba [][] ;

I don't really like it but as there aren't any confusion possible, there is no very big harm in letting them be equivalent. The rationale was that C and C++ coders were used to a certain notation :

 int a[];

while the recommendation in java is to consistently declare the type before as in

 int[] a;

Here's the reference : http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.2

And two extracts :

Brackets are allowed in declarators as a nod to the tradition of C and C++. The general rules for variable declaration, however, permit brackets to appear on both the type and in declarators, so that the local variable declaration

[...]

We do not recommend "mixed notation" in an array variable declaration, where brackets appear on both the type and in declarators.

In order to be more readable in Java, I suggest you stick to the usual

 Boolean[][] ba ;

Note that you have a similar behavior for method declarations. Here's an excerpt from the `ByteArrayOutputStream` class source code:

public synchronized byte toByteArray()[] {
    return Arrays.copyOf(buf, count);
}

This is allowed for compatibility but please don't use that. Most coders at first sight wouldn't notice the `[]` and thus wouldn't immediately read that this method returns an array.

Problem

I come across this code line in a book and it said this is legal , but I don't really understand though having googling. the code is: ``` Boolean [] ba []; ``` I just know that to create an array, it should be like this: ``` int [] numberArray; int numberArray []; int [] [] num2DArray; ``` Thanks!

Original source