• 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

Error or not Error with K&B?

 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic