Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B little bug

 
Lionel Orellana
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started with this book and I must say this is the one I should have bought first! It is much better than the Heller&Roberts guide in explaining what is to be found in the exam and what the real level is. I just hated the review questions-for-dummies in Heller&Roberts. The whole book left me with the impression that the exam was a piece of cake. Only the mock exams opened my eyes.
Anyway, to the point. Page 18, Chapter 1, Declaring an array. It reads:
Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.

The same thing is repeated in the Two-Minute drill at the end of the Chapter, page 43.
However, the following won't compile:
int a[], []b;
I believe it's better to say the brackets must be after the type or after the identifier (this of course I read somewhere else). Wording it this way makes it clear that b in the line above is not legal. The definition in the book would lead you think b is ok.
Cheers
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanation in the book is valid.
The brackets can be before or after the identifier but only when declared as a single statement.
int []b[]; -> Compiles
int []b; -> Compiles
int b[]; -> Compiles
Is the declaration int a[], []b; inside the book?
[ April 16, 2004: Message edited by: Thomas De Vos ]
 
Lionel Orellana
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas De Vos:
The brackets can be before or after the identifier but only when declared as a single statement.

That's the distinction they don't make. int a[], []b; is not in the book (at least not in the chapter I'm reffering to) and that's exactly my point: if you read a statement like "The brackets can be before or after the identifier" and then somewhere else (in the exam itself??) you see

int a[], []b;
you woud think this is legal.
 
Atanas Roussev
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well to make it a bit more confusing these one compiles just fine:

int a[], b[];
int a[][][][], b;
int a, b[];
int []a[], b[];
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the JLS says:

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both, as in this example:
byte[] rowvector, colvector, matrix[];
This declaration is equivalent to:
byte rowvector[], colvector[], matrix[][];

So looks wrong to me, because the [] before the identifier b is neither part of the type int (as in int []) nor part of the variable b (as in b[]).
 
Lionel Orellana
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it IS wrong. That's why I believe saying "brackets must be after the type or after the identifier" is a more accurate statement than "brackets can be to the left or right of the identifier" which is just not true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic