• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question from - danchisholm.net

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 http://www.danchisholm.net/july21/mybook/chapter4/exam1.html. 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..
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The [] bracket is not allowed after the comma.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi joe

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

a1[][]
a2[]
a3[][]
a4[][]][]

am i correct.... since all the variables are of type int array.
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paramesanwaran, apart from the extra ] you have in the a4 line, yes, I believe that's correct.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe,
Apart from the 'an' in paramesanwaran, everythings ok.
 
reply
    Bookmark Topic Watch Topic
  • New Topic