Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

OCA Java SE 8 Programmer I Study Guide - Chapter 2 - Review Question Confusion

 
Greenhorn
Posts: 18
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Today I would like to bring up a question I am confused about in the OCA Java SE 8 Programmer I Study Guide by Jeanne Boyarsky and Scott Selikoff. In the Chapter 2 Review Questions, I found #1 a little confusing.

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

I chose A, D, and F. However, the answer was supposed to be A and D. But <= is very alike == so why is answer F wrong?

Thanks,

Maxwell

 
author & internet detective
Posts: 39392
763
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x <= y is like a shorthand for x < y && x == y.

Let's think about this in English. The number 4 is clearly less than (or equal to) the number 5. The number 5 is also (less than or) equal to  the number 5. So <= works.

What would it mean for false to be less than than true? It wouldn't. So Java doesn't allow comparing booleans with <= because they can;t be substituted for x and y above.
 
Ranch Hand
Posts: 142
4
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

x <= y is like a shorthand for x < y && x == y.



I think It should be x <= y is like a shorthand for x < y || x == y. Because it is  x less than (or equal to) y but not x less than (and equal to) y .
Please correct me I am wrong.
 
Jeanne Boyarsky
author & internet detective
Posts: 39392
763
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narayana,
Yeah, I meant that . Thanks for pointing it out.

What I posted would always evaluate to false.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!