Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By benji2505

Thread: VERY SIMPLE boolean question

  1. #1
    Werdeyo is offline Member
    Join Date
    Jan 2017
    Posts
    6
    Rep Power
    0

    Default VERY SIMPLE boolean question

    Hey everyone I just started learning java and I'm just wondering how do I read this boolean statement.
    I know that it evaluates to false, because I ran it, but I don't know if you do the most inward parenthesis
    or if it's more difficult than that. Any help would be appreciated, thanks.

    Here it is:

    public class Precedence {
    public static void main(String[] args) {

    boolean riddle = !( 1 < 8 || (5 > 2 && 3 < 5));
    System.out.println(riddle);

    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: VERY SIMPLE boolean question

    Think of it like this:

    1 < 8 == true
    5 > 2 == true
    3 < 5 == true

    So you have

    not (true || (true && true))
    not (true || true)
    not true
    false

    Regards
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Werdeyo is offline Member
    Join Date
    Jan 2017
    Posts
    6
    Rep Power
    0

    Default Re: VERY SIMPLE boolean question

    So it is the parenthesis, thanks.

  4. #4
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    132
    Rep Power
    0

    Default Re: VERY SIMPLE boolean question

    Does boolean is being used only in comparisons ( <>= || .equals ).... ?
    I havent seen something like:
    Java Code:
    boolean control = false;

  5. #5
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,722
    Rep Power
    6

    Default Re: VERY SIMPLE boolean question

    Quote Originally Posted by asdfg View Post
    Does boolean is being used only in comparisons ( <>= || .equals ).... ?
    I havent seen something like:
    Java Code:
    boolean control = false;
    That is a valid use. You see this contrsuction more with running a Runnable (Thread):
    Java Code:
    boolean shouldStop = false;
    
    while (!shouldStop) {
      //do something 
      //that gets control
      //of something.
      if( someOperationThatReturnsABoolean() ) {
         shouldStop = true;
      }
    }
    As soon as the method "someOperationThatReturnsABoolean()" returns true, the "shouldStop" variable is set to true, effectively stopping the while loop since the condition for the while loop to run evaluate to false (!shoudStop).
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: VERY SIMPLE boolean question

    Not certain what you mean by "it was the parenthesis." The expression sans ! evaluated to true regardless of how you apply || or && or distribute the individual boolean expressions. The key was the ! symbol which turns a true expression false or false expression true.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    241
    Rep Power
    3

    Default Re: VERY SIMPLE boolean question

    Quick comment for the geeks: Java will not evaluate the right portion of the expression in the parenthesis. It is called short-circuiting. The reason behind it: if you have an expression "true || ???" the result will always be true, independent from ???. The same holds true for an expression "false && ???".
    jim829 likes this.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: VERY SIMPLE boolean question

    Good point! And a corollary to that is that you can use | or & to defeat short circuiting (although I rarely do).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Werdeyo is offline Member
    Join Date
    Jan 2017
    Posts
    6
    Rep Power
    0

    Default Re: VERY SIMPLE boolean question

    Sorry if I don't fully understand but to me it looks like after you simplified the operators and operands
    (I don't know if that's a good way of explaining things) but after you did that, to me, it looked like you
    did what was on the inner most parenthesis and then repeated that and then finally took into account the
    !not operator.

    I also thought I read that you have to do the logical operators (!, ||, &&) in a certain order but I don't know if that's true

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: VERY SIMPLE boolean question

    Yes that is what I did. I also recommend you read about de Morgan's laws. They come in handy sometimes to help better understand an expression.

    And don't forget about exclusive or (using a ^ sign). That says the expression is true if one or the other is true, but not both. So the following prints out false, true, true, false.

    Java Code:
          System.out.println(true ^ true);
          System.out.println(true ^ false);
          System.out.println(false ^ true);
          System.out.println(false ^ false);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 01-16-2014, 02:48 PM
  2. Replies: 6
    Last Post: 09-19-2013, 03:06 AM
  3. paint error, with a simple boolean
    By PRW56 in forum New To Java
    Replies: 16
    Last Post: 04-05-2012, 09:08 AM
  4. my simple boolean code keeps saying true
    By shazakala in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 10:30 AM
  5. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 06:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •