The Artima Developer Community
Sponsored Link

Java Answers Forum
Rookie question on Boolean operators

1 reply on 1 page. Most recent reply: Aug 14, 2006 2:47 AM by Dave Hinton

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Guido

Posts: 38
Nickname: kiethb
Registered: May, 2002

Rookie question on Boolean operators Posted: Aug 10, 2006 7:31 AM
Reply to this message Reply
Advertisement
Can someone please explain the difference between the following two snippets? I am using a short circuit && and a non-short circuited | at the same time to evaluate these conditions.


int x = 2;
int y = 5;
if (( x > 3 ) && ( y < 2 ) | boo() ){
Print(“You got this right”);
}
public boolean boo(){ return true; }


I understand that this logic is saying that x must be greater than 3 AND EITHER y has to be less than 2 OR boo must be true. weird but no problems there. however, in the following i have made a change to a short circuited OR...


int x = 2;
int y = 5;
if (( x > 3 ) && ( y < 2 ) || boo() ){
Print(“You got this right”);
}
public boolean boo(){ return true; }


Can someone tell me the logic of why this is being evaluated like there are now parenthesis around the first two arguments like this: ((x>3) && (y<2)) ??? this is how i would have thought the first example would have worked but to my dismay it did not. just having trouble visualizing the effects when mixing short circuits and non-shorties...

any help will be much appreciated! thanks!


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: Rookie question on Boolean operators Posted: Aug 14, 2006 2:47 AM
Reply to this message Reply
Different operators have different precedences. Find a java operator precedence table, you will find that these operators are in different places.

You have found that it is difficult to understand code that mixes boolean short-circuiting and non-short-circuiting operators in the same line… so don’t do it.

Flat View: This topic has 1 reply on 1 page
Topic: PAM & Java Previous Topic   Next Topic Topic: java.util.Collection type casting

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use