File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes precedence question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "precedence question" Watch "precedence question" New topic

precedence question

Mindy Hudson

Joined: Dec 08, 2000
Posts: 28
class Question {
public static void main(String[] args) {
int x = 1;
boolean b1,b2,b3,b4;
x=(b1 | b2 & b3 ^ b4) ? x++ : --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.
James Baud
Ranch Hand

Joined: Jan 06, 2001
Posts: 60

AND & (highest)
OR | (lowest)
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

<B>~James Baud</B><P>He who asks, is a fool for five minutes;<BR>but, he who does not ask, remains a fool forever. (Chinese proverb)
Mindy Hudson

Joined: Dec 08, 2000
Posts: 28
thanks james.I thought all of them had same precedence.
Sam Wong
Ranch Hand

Joined: Dec 07, 2000
Posts: 133
Mindy, here's a link that shows the order of precedence for all the operators with respect to each other.

[This message has been edited by Sam Wong (edited January 17, 2001).]
Mindy Hudson

Joined: Dec 08, 2000
Posts: 28
sam the link was real useful
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
There is expression:
Addicts____Arithmetic and shift
Like_______Logical and bitwise
A lot______Assignments
Hope it helps
[This message has been edited by Vladimir Kositsky (edited January 17, 2001).]
I agree. Here's the link:
subject: precedence question
It's not a secret anymore!