Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding boolean behavior  RSS feed

 
Mike Stein
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • 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?
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
    • 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.
     
    Campbell Ritchie
    Marshal
    Posts: 55751
    163
    • Mark post as helpful
    • send pies
    • 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: 55751
    163
    • Mark post as helpful
    • send pies
    • 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: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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
    • 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: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!