Win a copy of Fixing your Scrum this week in the Agile 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Frits Walraven
  • Himai Minh

OCP 11 - Complete Study Guide - Chapter 2 review questions

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, I have started to study for OCP 11 certification using the OCP 11 complete study guide. In chapter 2 one question and its explanation confused me. The question says:

Which of the following Java operators can be used with boolean variables? (Choose all that apply.)
A. ==
B. +
C. --
D. !
E. %
F. <=
G. Cast with (boolean)

I chose A and D logically but the answers say A, D, G. In the answer, it explains like we can cast a boolean variable since boolean is a type but how can we cast other primitive types to boolean?

author & internet detective
Posts: 40709
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
Welcome to CodeRanch!

You can't. The intent of saying cast with boolean is to rule out other types. The option means something like this:

Master Rancher
Posts: 1011
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
I hadn't seen this one before, I missed it.

I had falsely memorized that boolean can never be used in a cast, and got a question or two wrong on mock exams, because that is not QUITE true.

I later memorized that this is legal (trivial boolean cast):
jshell> boolean b = true
b ==> true

jshell> boolean another = (boolean)b
another ==> true

But still forgot the other dumb case where we combine un-boxing and the cast like Jeanne showed us here:

jshell> Object o = true
o ==> true

jshell> boolean yetAnother = (boolean)o
yetAnother ==> true

So the rule is nothing can be cast to or from a boolean except another boolean or an Object which has an actual type of Boolean

Lastly, something like this compiles but will throw a ClassCastException at Runtime:
jshell> Object oBad = 2468
oBad ==> 2468

jshell> boolean oneMoreBoolean = (boolean)oBad
|  Exception java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Boolean (java.lang.Integer and java.lang.Boolean are in module java.base of loader 'bootstrap')
|        at (#9:1)

No, this is the last case, you always need to be careful of possible Null values using wrapper types:
jshell> Boolean bNull = null
bNull ==> null

jshell> boolean ohBoy = (boolean)bNull
|  Exception java.lang.NullPointerException
|        at (#11:1)

Auto-boxing and unboxing are fun, easy and cool and our friends until they bite us in the butt for carelessness.
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic