Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Errata OCA Java SE 8 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Miguel Callejón
Greenhorn
Posts: 4
Android Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I am preparing myself for the OCA exam and looks like I've found an errata:

Chapter 5, page 282:
It says "Here are some basic rules to keep in mind when casting variables", but we are talking about casting objects, aren't we?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Miguel Callejón,

First of all, a warm welcome to CodeRanch!

Miguel Callejón wrote:Chapter 5, page 282:
It says "Here are some basic rules to keep in mind when casting variables", but we are talking about casting objects, aren't we?

I think you are incorrect and the study guide is spot-on! You cast reference variables from one type to another. And the compiler will verify if the cast is allowed (e.g. you can not cast a Dog to a Cat, or an Integer to a Long). And when the code is executed, the type of the actual object (referred by the reference variable) and the type of the cast will determine if you'll get a ClassCastException at runtime or not. If the type of the actual object and the type of the cast are compatible, the cast will complete successfully at runtime; otherwise a ClassCastException is thrown.

Also please note that the compiler does not execute any code at all! So to verify if a given cast is valid, the compiler only knows the type of the reference variable and the type of the cast, it doesn't know (and care) about the type of the actual object (the reference variable is referring to).

Casting and instanceof is probably one of the more popular topics in this forum. So using the search function you'll probably find a bunch of (very) good topics on this exam objective. These topics contain all valuable information (with code snippets to illustrate rules and possible pitfalls) about casting:
  • Why will this throw Class Cast Exception?
  • Casting
  • Not sure why my answer on overloading was incorrect
  • Some doubts about casting
  • instanceof operator with an interface versus class


  • Hope it helps!
    Kind regards,
    Roel
     
    Miguel Callejón
    Greenhorn
    Posts: 4
    Android Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the explanation and for the links, Roel! It was indeed strange that nobody didn't notice that errata before, but I wanted to ask anyway.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Miguel Callejón wrote:It was indeed strange that nobody didn't notice that errata before, but I wanted to ask anyway.

    No problem! It is much better to ask if you have any doubts than to learn incorrect concepts. And the moose says we are a friendly place for programming greenhorns, thus (very) happy to help you and clear your doubts
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic