Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

wrong cast on page 65, chapter 2 (Java OCP 8 Programmer II Study Guide, Sybex)

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
page 65


Should the DOES NOT COMPILE line be:
Bird bird = (Bird) fish;

Since the point of the example is to show 2 unrelated classes can't be cast to each other.
As written, of course the code won't compile but not for the reason listed.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roy Pozarelli wrote:Since the point of the example is to show 2 unrelated classes can't be cast to each other.
As written, of course the code won't compile but not for the reason listed.


From the code snippet you would expect that it doesn't compile for another reason, but when you try to compile this code snippetyou'll get exactly the same reason listed in the paragraph (incompatible types)So against all odds, the study guide is actually spot-on!

Roy Pozarelli wrote:Should the DOES NOT COMPILE line be:
Bird bird = (Bird) fish;


But I agree it would make more sense and be much more obvious, if the code snippet tries to cast the reference variable fish to Bird. Because with the current code snippet you might think it doesn't compile because the reference variable bird may not have been initialized (and the first line of the main method is redundant). So the statementshould be changed toAnother alternative could be this main method

Hope it helps!
Kind regards,
Roel
    Bookmark Topic Watch Topic
  • New Topic