• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I landed here in this nice forum because I also think the statement is not precise.
The restriction appears to be related to declaring variables, as described in Chapter 2, section "Declaring Multiple Variables".
Just like throws an error

throws an error, too

The following is just fine:
 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed with the original and latest posters, as well as Campbell.

The certification exam problems (real and mock) aren't always a source of best practices, because of their focus on what is legal, rather than best practices.

Certification exam prep materials are not always the best way to learn a new topic, for the same reason.

However, in this forum, it is reasonable to focus on clear statements about what is and is not legal / will and will not compile.

The examples of pathologically complex yet legal initializers in for loops, with the variables declared outside the loop will indeed compile, and the sentences referred to can cause one (in isolation, they show a counter-example elsewhere) that they would not.

In the extensions to try-with-resources syntax where one is now allowed to declare the variables outside the try-with-resources (  ), and to just name them inside the (  ), the Sybex books go out of their way to show that you can do it but you might not want to, as if you do that with more than one resource, you can wind up with a resource leak, defeating the whole purpose of the try-with-resources construct in the first place.

The examples in this thread are less pathological, or at least less dangerous, than the ability to declare a resource prior to a try-with-resources.  It might be good to fix the wording of that sentence.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic