• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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: 39343
755
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: 39343
755
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!