Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boolean

 
Spandan Pandey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!!
I was just having trouble understanding how the boolean works in Java.I am not really getting the logic..the idea is a bit vague.I would be glad if you made it a bit clear for a novice like me.
For example in this program:

 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought to use code tags, but I have added them for yoiu. Coloured text doesn’t work inside code tags. It looks a lot better () and would have been better still with correct indentation.
Is that example from Head First Java? Which page? Have you copied it correctly? I am sure Sierra & Bates would never use == true, and neither should you; it is poor style and error‑prone in case you write = by mistake.
The idea of the condition is to show that the bangbang‑a‑bang method is not called when that flag is set to false. In line 3 it becomes true, and later (line 15) it becomes false.
 
Spandan Pandey
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You ought to use code tags, but I have added them for yoiu. Coloured text doesn’t work inside code tags. It looks a lot better () and would have been better still with correct indentation.
Is that example from Head First Java? Which page? Have you copied it correctly? I am sure Sierra & Bates would never use == true, and neither should you; it is poor style and error‑prone in case you write = by mistake.
The idea of the condition is to show that the bangbang‑a‑bang method is not called when that flag is set to false. In line 3 it becomes true, and later (line 15) it becomes false.


Yeah,it's from head first java.Pg 46..it's from an exercise code magnets!!
so,what should one use instead of "== true"?

Thanks I didn't know how to use code tags that time.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have a bolean, you can just do this:

 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spandan Pandey wrote: . . .
so,what should one use instead of "== true"?
What Mr Rosenberger said.
Instead of == false, use if (!myBoolean)...


Thanks I didn't know how to use code tags that time.
You’re welcome

And now I have found my copy of HFJ and p46, I am surprised to see they used == true there
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic