jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean" Watch "Boolean" New topic
Author

Boolean

Spandan Pandey
Greenhorn

Joined: Feb 20, 2013
Posts: 10
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

Joined: Oct 13, 2005
Posts: 37874
    
  22
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

Joined: Feb 20, 2013
Posts: 10
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

Joined: Oct 02, 2003
Posts: 11139
    
  16

if you have a bolean, you can just do this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
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.
 
subject: Boolean
 
Similar Threads
Error: Exeption in thread "main" java.lang.NoSuchMethodError: main
Head First Exercise Pg 43
Method help: how does this work?
Wrong coding
Boolean question