• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

precedence question

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Question {
public static void main(String[] args) {
int x = 1;
boolean b1,b2,b3,b4;
b1=b2=b3=b4=true;
x=(b1 | b2 & b3 ^ b4) ? x++ : --x;
System.out.println(x);
}
}

When I run this code I get 1 as answer I thought The expression in braces give false and so --x will get executed and we will get 0.CAn someone explain.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Precedence<pre>
AND & (highest)
XOR ^
OR | (lowest)
</pre>
x=(true | ((true & true) ^ true)) ? x++ : --x;
x=(true | (true ^ true)) ? x++ : --x;
x=(true | false) ? x++ : --x;
x=true ? x++ : --x;
------------------
~James Baud
Talk, does not cook rice. - Chinese Proverb
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks james.I thought all of them had same precedence.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mindy, here's a link that shows the order of precedence for all the operators with respect to each other.
http://www.isi.edu/~frank/javaOperatorPrecedence.html

[This message has been edited by Sam Wong (edited January 17, 2001).]
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sam the link was real useful
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is expression:
Ulcer______Unary
Addicts____Arithmetic and shift
Really_____Relational
Like_______Logical and bitwise
C__________conditional
A lot______Assignments
Hope it helps
[This message has been edited by Vladimir Kositsky (edited January 17, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic