• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Array Declaration  RSS feed

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can declare any array like
int []abc ,def[];
But following declation throws compilation error...<identifier> expected
int []abc ,[]def;
Can somebody justify?
Cheers!
Swapnil
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting.

These do compile:

int []abc ,def[];
int []abc[] , def[][];

I guess what these questions really are about is: how does the java language specify compiler implementation?

Here, I guess that an array is allowed to have dimensions float- but only to the right of the type, or to the right of an identifier. Commas delimit identifiers. Thus, after the comma it expects to see an identifier, and doesn't find one, so complains.

These do not compile:
[] int abc[] , def[][];
int ab[] c , def[][];


To really know the answers to these you would want to see the regular expressions which define the language.

A related question is- how many dimensions does c have here?

int [][] ab[], c , def[][];

2 I believe- the int [][] says a 2 dimensional array. ab has 3 and def had 4.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Tom Tolman:
int [][] ab[], c , def[][];



My testing shows that this is correct: ab has 3 dimensions, c has 2, and def has 4. Quite unexpected (to me, anyway)! Thanks for posting this!

Some generalizations:

In a multi-dimensional Array declaration, the identifier can appear anywhere among the pairs of brackets. In other words, the bracket pairs can be "split up" by the identifier...

int arr[][][];
int []arr[][];
int [][]arr[];
int [][][]arr;

However, the identifier cannot be split up by the brackets...

int []a[]rr[]; //illegal!

Nor can the identifier be inside brackets...

int [arr][][]; //illegal!

Now, here's where it gets interesting...

Apparently (at least with Sun's compiler), an Array of a given type and dimension can be declared, then any subsequent identifiers on that line can add dimensions -- provided that the brackets indicating the additional dimensions come after the associated identifier.

int[]a, b, c[], d[][], e, f[][][];

The above line declares a one-dimensional int Array. The first of these is identified by a, and the second by b. Then c adds a dimension, so is a 2-dimensional int Array. Then d adds 2 dimensions, so is a 3-dimensional int Array. Next, e does not add any dimensions, so it is only 1-dimensional (demonstrating that these additional dimensions are not cumulative). Finally, f adds 3 dimensions, resulting in a 4-dimensional int Array.

Note that we can add g[] to the above declaration, but we can't add []g...

int[]a, b, c[], d[][], e, f[][][], g[];
int[]a, b, c[], d[][], e, f[][][], []g; //illegal!

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting points Marc. It seems you solved the puzzle. I had no idea arrays behaved this way.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Tom Tolman solved the puzzle. I just verified his results.

But this is weird...

Look at the declaration line in the code below. On a single line, i is declared as a 2-dimensional Array (an Object), while j remains a primitive int!



[ September 03, 2004: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As illustrated above, the single line...

int i[][], j;

...declares i as a 2-dimensional Array (an Object), while j remains a primitive int.

So I'm wondering: Is this documented as an acceptable shortcut? Or is this actually a glitch that we should avoid using -- in case it gets "fixed"?

(Or... Am I the only one who sees this as peculiar?)


[ September 04, 2004: Message edited by: marc weber ]
 
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Languages are not defined by regular expressions, they are defined by grammars. All is explained if you understand how the Java grammar, as defined in the Java Language Specification (JLS), handles the two styles of brackets differently.

The really short answer is that you are reading the declarations wrong. Informally speaking, "int[]a" should be read as "(int[]) a" and not as "int ([]a)".

To be more precise, we need to look at the Java grammar. Please read the following only if you are interested in BNF grammars, skip to the end if you don't know what a grammar is but would still like to find out a little more.

---- 8< ---- cut here ---- 8< ---- cut here ---- 8< ----

The first style includes the brackets in the array type itself: ArrayType production is:

ArrayType: Type [ ]

Because ArrayType itself is part of the Type production, you can get multiple dimensions by multiple applications of this production. So valid ArrayTypes might beThe second style groups the brackets with the variable you are declaring:

VariableDeclarator: Identifier VariableDeclaratorRest
VariableDeclaratorRest: BracketsOpt [ = VariableInitializer]
BracketsOpt: {[]}

In a grammar, [ foo ] denotes optionality, { foo } denotes zero or more repetitions. So valid VariableDeclarators areNow the crux of the matter is the way in which the Java grammar puts these two ingredients together:

LocalVariableDeclarationStatement:[final] Type VariableDeclarators ;
VariableDeclarators: VariableDeclarator { , VariableDeclarator }

A variable declaration is a Type followed by one or more VariableDeclarators, and either may specify brackets to denote an array type. The scope of their effect is different, though; if you do it in the Type, it applies to all VariableDeclarators that follow. If you do it in the VariableDeclarator then only that variable is affected.

---- 8< ---- cut here ---- 8< ---- cut here ---- 8< ----

In other words, a variable declaration starts with a type which is the starting point, so to speak, for all variable declarators that follow. InThat type is int[]. The subsequent variables (the VariableDeclarator production) may modify the basic type with additional array brackets, so "a" is an int[], "c[]" is an array of int[]s and so on.

Your illegal codeIs illegal simply because the JLS grammar does not allow you to prefix a variable name with brackets. It only allows the brackets as suffixes to type names and identifiers. You really ought to use spacing to make that clear:Because this is all clearly laid down in the JLS, this is not a quirk in the Java compiler. It's a feature. There's no difference between Java implementations here. Use this knowledge without worry - but keep your code readable please

- Peter
[ September 06, 2004: Message edited by: Peter den Haan ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter den Haan:
The really short answer is that you are reading the declarations wrong. Informally speaking, "int[]a" should be read as "(int[]) a" and not as "int ([]a)".


Actually, what surprises me is that both ways of reading this seem to apply.

I'm okay with specifying the type as an array of any dimension, and then adding dimensions by using the optional brackets. I was initially surprised by this, but I see that it's logical. After all, these identifiers will all represent arrays that hold a specific type of data, so the fundamental type isn't changing.

But what I'm questioning is specifying the type as something other than an array -- even a primitive -- then apparently changing that type to an array by adding brackets. This is illustrated by the code below.



In any case, thanks for the links on Java grammar. I've been looking through this material, which is why it took me a couple of days to respond. I still don't see how this particular situation is addressed, unless I'm confused about something as fundamental as type.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] b;
Should be read as: declare a reference variable b, that points to a one dimensional int[] array.

int b[];
Here it should be read as: declare a one dimensional array of the specified type type int.

int[][] b[];
Should be read as: declare a one dimensional array of the specified type int[][] -> an array holding other 2 dimensional int arrays

So you don't actually have to think of it as adding dimensions, you declare an array of the specified type.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!