• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Logical operators

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true that '&&' and '| |' have equal precedence
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im not sure i understand your question
they are both short circuit operators what do you mean by equal
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't really a Threads and Synchronization question, so I've moved it to Java in general (beginner).
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to my notes the precedence of && is slightly higher than that of | |.
 
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a conditional statement && and | | must both be evaluated, unless a short-circuit conditon is detected in that case the rest of the condition is ignored. The precedence (if you want to use that word) is linear.
Example: if this_thing is true | | that_thing is false && the_other_thing is true
If this_thing is true the rest of the statement is ignored.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
IMO && has a higher precedence than | |. the JLS does not say anything with respect to this i checked this out here.
http://java.sun.com/docs/books/jls/html/15.doc.html#5247
the code below helps me on this.

class demo22
{
public static void main(String[] args)
{
boolean b=false;
boolean a=true;
boolean c=false;
if(a| |b&&c) System.out.println("second");
}
}
let us start with three hypothesis
1) | | has higher precedence
2) evaluation is from left to right
3) && has higher precedence
if | | has higher precedence then the output of (a| |b&&c) must be
true| |false which is true && false which is false. Hence second must not be printed out. same is the case for evaluation from left to right.
if && has higher precedence then b and c must be evaluated first and the result is false. then the | | is evaluated wherein the result is true. hence in this case it should print out second.

please check out the example and see for u r self!!
Hope my logic is correct.
rahul.
 
Ray Marsh
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are probably correct Rahul. It sounds right.
Conditional statements should be easily understood. If you have to refer to a rule of precedence for interpreting code, it's probably too complex. Unless you're very new to programming.
Precedence is more important for mathematical calculations, even then you should use parenthesis to not only make the code clear, but insure the results you want.
Simplify by using nesting techniques and or boolean variables.
reply
    Bookmark Topic Watch Topic
  • New Topic