wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B little bug 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 "K&B little bug" Watch "K&B little bug" New topic

K&B little bug

Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
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.
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
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 ]

Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
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

Joined: Apr 07, 2004
Posts: 33
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

Joined: Aug 03, 2002
Posts: 7729
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[]).

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
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.
Have you checked out Aspose?
subject: K&B little bug
It's not a secret anymore!