aspose file tools*
The moose likes Beginning Java and the fly likes Error or not Error with K&B? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error or not Error with K&B?" Watch "Error or not Error with K&B?" New topic
Author

Error or not Error with K&B?

Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Hello everyone here,

I have a few question, if somebody can resolve then, i will very glad with him/her

Question 1.

�How many errors had found on the book K&B?

Question 2.

topic: The Enhanced for Loop

See the next code.



The book K&B say: that is ilegal, buy i can compile fine.
this info is between the pages 339 - 340

Thank you.

PD. I dont know if testing compile on JAVA 5 it will not compile.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

My JDK 6u3 refuses to compile this.

I don't know why Sun have done it, but the iteration variable must always be declared inside the loop itself. You can't use a previously declared variable.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Thankyou Rob Prime...

Its very rare for me, when i try to compile again, the file no compile.

Thank you!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Rob']: I don't know why Sun have done it, but the iteration variable must always be declared inside the loop itself. You can't use a previously declared variable.

I guess because there's really no benefit to reusing an existing variable here (unless perhaps you're doing a lot of deep recursion and are reunning out of stack space). Any previous value of the variable will be overwritten anyway, and in general it just makes the code more confusing if the variable is declared away from where it's used. I guess they're just encouraging good style.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Error or not Error with K&B?