• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boolean

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you have a bolean, you can just do this:

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic