• 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

Question on book - Oracle Certified Associate Java SE 8 Programmer I by Jeanne Boyarsky

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I primarily intend to ask this question to authors of the book -"OCA Oracle Certified Associate Java SE 9 Programmer I STUDY GUIDE" by Jeanne Boyarsky and Scott Selikoff.

On page no 82 of this book, there is a section that explains the for loop and under it the point 4 says "Using Incompatible Data Types in the Initialization Block" which basically explains that the initialization block of the for loop cannot have incompatible types. For example:



The question I have is the below code compiles even though it has Incompatible data types:


Above example can also be considered as having incompatible types, right ?


BTW I must say that so far reading of "Oracle Certified Associate Java SE 8 Programmer I" has been thoroughly enjoyable. Thanks to the authors - Jeanne and Scott

 
author & internet detective
Posts: 41878
909
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
Santosh,
Welcome to CodeRanch! I'm glad you are enjoying the book. Note that I've moved this thread to the OCAJP 7/8 forum. That's where any future questions about the book should go.

That rule (about the same type) only applies to type declarations. If the type was declarated elsewhere, different types are allowed.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

When you post code, it helps to UseCodeTags (← that's a link). I'll do it for you this time.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Santosh Kumar Nv,

First of all, a warm welcome to CodeRanch!

Santosh Kumar Nv wrote:Above example can also be considered as having incompatible types, right ?


That only applies if you declare variables in the initialization block of the for loop. But if you only initialize variables in the initialization block of the for loop, you can combine different types as illustrated in the following example:
And what do you think about this code? Does it compile or not? And if it doesn't, why not

Hope it helps!
Kind regards,
Roel
 
Santosh Kumar Nv
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

Thanks for your comment. Regarding your question

And what do you think about this code? Does it compile or not? And if it doesn't, why not



This code cannot compile because 'size' is a duplicate variable now(One is declared outside the loop and one in the initialization block of the for loop). Also as you know we cannot declare multiple types in the same line using comma as a de-limiter it should be semi-colon(;).

So the below declaration(my original post -from the Certification book I mentioned earlier) doesn't actually declare long or int variables. Such syntax is not possible in "for" loop or in fact in Java language. This makes me believe that the error we get with the below for loop is not with trying to initialize multiple in-compatible types in the "for" loop. But, it is because the syntax used is in-correct according to Java language. What do you say ?



Regards,
Santosh
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Santosh Kumar Nv wrote:This code cannot compile because 'size' is a duplicate variable now(One is declared outside the loop and one in the initialization block of the for loop).


You are correct about that one!

Santosh Kumar Nv wrote:Also as you know we cannot declare multiple types in the same line using comma as a de-limiter it should be semi-colon(;).

So the below declaration(my original post -from the Certification book I mentioned earlier) doesn't actually declare long or int variables. Such syntax is not possible in "for" loop or in fact in Java language. This makes me believe that the error we get with the below for loop is not with trying to initialize multiple in-compatible types in the "for" loop. But, it is because the syntax used is in-correct according to Java language. What do you say ?


But you are wrong about this one! In the initialization block of the for loop, you must use a comma as delimiter to declare different variables of the same type as the following code snippet illustratesAnd from this code snippet shows clearly that the same applies to the increment block of the for loop as well.

Hope it helps!
Kind regards,
Roel
 
Santosh Kumar Nv
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Roel,

Thank you again for your quick response!!!
By multiple types I actually meant using different data types as comma separated in a single statement. I think, such syntax in itself is not possible in Java, so I don't see any point using it in for loop.

For example:

fails irrespective of it is used in normal Java statement or in a for loop. Now going back to my original question again - Can such an attempt in for loop be considered as "Using Incompatible Data Types in the Initialization Block" in the OCA book is misleading. Further it says using "The variables in the initialization block of the for loop must all be of same type." After trying few examples, I differ with this statement. However, I am happy with original explanation given by Jeanne Boyarsky -

That rule (about the same type) only applies to type declarations. If the type was declarated elsewhere, different types are allowed.



Regards,
Santosh
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Santosh Kumar Nv wrote:By multiple types I actually meant using different data types as comma separated in a single statement. I think, such syntax in itself is not possible in Java,


You are correct! If you declare (and/or initialize) variables of the same type in a single statement, you can use a comma as delimiter. If you do the same with different data types, you'll get a compiler error. Illustrated in this code snippet:

Santosh Kumar Nv wrote:so I don't see any point using it in for loop.


So when you encounter it in a for loop, you'll know what to do: check the Compilation fails answer and move on to the next question

Santosh Kumar Nv wrote:Now going back to my original question again - Can such an attempt in for loop be considered as "Using Incompatible Data Types in the Initialization Block"


Yes, it can! It illustrates you can't declare different data types in the initialization block of a for loop. Just like it's impossible to declare (and/or initialize) variables with different data types in a single Java statement (seperated with comma). But it only applies to the declaration of variables of different data types; if variables with different data types are already declared prior to the for loop, you can initialize them all at once in the initialization block of a for loop (as the code snippet in this post illustrates).

Santosh Kumar Nv wrote:Further it says using "The variables in the initialization block of the for loop must all be of same type." After trying few examples, I differ with this statement.


True! As already confirmed by Jeanne (one of the authors) and in the aforementioned post that rule only applies to variable declarations, not to the initialization of variables declared prior to the for loop. So that could maybe be slightly rephrased in the next version of the study guide.

Hope it helps!
Kind regards,
Roel
 
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic