It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question from - Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question from -" Watch "A question from -" New topic

A question from -

deepu Bhalotia
Ranch Hand

Joined: Apr 19, 2005
Posts: 39
class MWC212 {
public static void main(String[] args) {
int[] a1[],a2[]; // 1
int []a3,[]a4; // 2
int []a5,a6[]; // 3
int[] a7,a8[]; // 4

This question is given in They tell that the line number-2 will generate a compile time error.

Can anybody explain why line-2 will generate a compile time error. If line-2 makes a compile time error why not line-3..
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
The [] bracket is not allowed after the comma.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
The [] brackets must come directly AFTER a type or variable name. Either they come directly the after the type, as in
int[] a1;
or they come directly after the variable name, as in
int a1[];
So when you start declaring multiple variables in one statement, the legal places to put the [] include these:
int[] a1[], a2, a3[], a4[][];
The spacing is flexible, so these are fine:
int [] a1 [], a2,a3[],a4[] [];
int []a1, a2,a3 [],a4 [][];

What you CAN'T do is put the brackets directly after a comma:
int[] a1, []a2; // Compiler error: <identifier> expected

SCJA 1.0 (98%), SCJP 1.4 (98%)
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi joe

in this case int [] a1 [], a2,a3[],a4[] [];
the actual representation is


am i correct.... since all the variables are of type int array.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Paramesanwaran, apart from the extra ] you have in the a4 line, yes, I believe that's correct.
yoga eesh

Joined: May 03, 2005
Posts: 19
Apart from the 'an' in paramesanwaran, everythings ok.
subject: A question from -
It's not a secret anymore!