Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error or not Error with K&B?

 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Rob Prime...

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

Thank you!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic