Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Joe Ess
  • salvin francis
  • fred rosenberger

Statement in OCP Java11 guide book may be better phrased. Chapter 4

Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 4: point 4 under for loop.
4. Using Incompatible Data Types in the Initialization Block

Like the third example, this code will not compile, although this time for a different reason. The variables in the initialization block must all be of the same type. In the multiple terms example, y and z were both long, so the code compiled without issue, but in this example they have differing types, so the code will not compile.

The above statement suggests that the below code should also not compile, whereas it does:

It's just my suggestion, as I was confused when I was trying various permutations myself as to understand the for loop better.
Posts: 67533
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the second code block, you have declared the loop variables in advance; that might not be such good design because the variables' scope is larger.
It might be better style to initialise the long variable to 0L.
Please tell us which sentence you think is phrased poorly, and the full details of the book inluding authors.
You won't learn to understand a for loop by looking at cert exam book examples. Practise with some “normal” loops with one variable only, iterating an array. Show us your code and let us see that we think of it.
author & internet detective
Posts: 39790
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's from my book.

I think your example is more like our example #2:

Which we note does compile because the type is declared outside the loop. (well for one of the two)
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!