Win a copy of 97 Things Every Java Programmer Should Know 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 39996
807
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: 145
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: 39996
807
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.
 
You can't expect to wield supreme executive power just because
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic