• 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

Understanding boolean behavior

 
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am having a bit of trouble understanding how booleans work. In the code snippet below I have a boolean variable that is not defined. I have read that when booleans are not defined, they default to false.


I am confused by the if statement. If booleanTest is false, why then does the sys.out statement in the if-statement print when I run this code? Wouldn't booleanTest have to be true in order for this statement to print? I never set booleanTest to true so I don't understand why it is printing what is in the if-statement. Could anyone explain to me what is going on?
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Stein wrote: I have read that when booleans are not defined, they default to false.  


  • Yes when variables that are declared in class but not initialized will be set to their respective default value by the compiler. Remember local variables means variables which are declared in constructor, method, initializer must be initialized before their use, they are not set to default value.


  • I am confused by the if statement. If booleanTest is false, why then does the sys.out statement in the if-statement print when I run this code? Wouldn't booleanTest have to be true in order for this statement to print?


  • In your example booleanTest is declared in class but not initialized Or assigned any value as you know default value of boolean is false so booleanTest is false but Logical complement operator ! inverts the value of a boolean so !booleanTest  means invert of false i.e. true but It doesn't change the value of booleanTest hence expression of If statement becomes true and print statement inside If gets executed which prints value of booleanTest i.e. false

  • Let's see another example.
    Example:Output:
    1: flag = false
    2: !flag = true
    3: flag = false
    5: flag = false
    After assigning true:
    6: flag = true
    7: !flag = false
    8: flag = true
     
    Mike Stein
    Ranch Hand
    Posts: 33
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Ganesh,

    Thank you for this explanation! It really does help to clear up some of my confusion. Just one more quick question, why, if in your example as in mine the non initilazied variable is set to false, does this code snippet not execute?



    What it is effectively saying is "if flag is false print the following sys.out statement", but it doesn't execute even though the flag variable defaults to false. This bit I don't understand.
     
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mike Stein wrote:. . .  "if flag is false print the following sys.out statement", . . . .

    No, it says, “if flag is true print the following sys.out statement.” You forgot the bang sign ! for NOT.
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Mike Stein wrote:. . .  why . . . does this code snippet not execute? . . .

    It does execute. But only as far as if (...).
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The expression of If statement needs to evaluate to true then and then only body of if statement gets executed. Read here --->The if-then Statement

    In below example value of flag is false means expression of If evaluated to false so body of If statement i.e. code within { } is not executed which contains print statement

    In below code we have ! (NOT) operator with flag which is nothing but !false, in simple english NOT false means true so expression evaluates to true then body of if statement get executed which prints value of flag.

     
    Mike Stein
    Ranch Hand
    Posts: 33
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    All,

    Thank you so much for the explanation. I understand if statements much better now. It didn't occur to me that the if statement would only fire what was inside the brackets unless the value was true. Again, thanks for the help! You guys are the best!
     
    Ganesh Patekar
    Bartender
    Posts: 1251
    87
    Hibernate jQuery Spring MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
     
    I found a beautiful pie. And a tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic