aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator precedence 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 "operator precedence" Watch "operator precedence" New topic
Author

operator precedence

cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Hi,
I would like to be sure. On one mock test, if found this as explanation of a question:
since (b1|b2&b3^b4) evaluates as (b1 | (b2&(b3^b4))) then ...
Is this correct??
I thought because of operator precedence & ^ |
(b1|b2&b3^b4) evaluates as (b1|((b2&b3)^b4)))
Am I wrong?
Thanks for your response,
Cyril.


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247

I thought because of operator precedence & ^ |
(b1|b2&b3^b4) evaluates as (b1|((b2&b3)^b4)))


sorry, must be changed in:
(b1|b2&b3^b4) evaluates as (b1|((b2&b3)^b4))
Ben Ethridge
Ranch Hand

Joined: Jul 28, 2003
Posts: 108
I realize I'm new to javaranch, so please tell me if I'm missing something:
These are all bitwise operators. Doesn't java evaluate them left-to-right, as in:
((b1|b2)&b3)^b4) ?
Note example:
class BenOps {
public static void main(String []args){
int i1=1;
int i2=2;
int i3=4;
System.out.println(i1|i2);//3
System.out.println(i2&i3);//0
System.out.println(i1|i2&i3);//1
System.out.println((i1|i2)&i3);//0
System.out.println(i1|(i2&i3));//1
}
}
If it helps others, this is a little mnemonic I devised to help me remember the precendence:
UnArShComBiShConAss
Unary
Arithmetic
Shift
Comparison
Bitwise
Short Circuit (|| &&)
Conditional (?boolean a:b)
Assignment
Respectfully,
Ben
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Hi Charles,

These are all bitwise operators. Doesn't java evaluate them left-to-right

I'm not sure at all. Look at this very simple example:
class Test{
public static void main(String arg[]){
boolean b1 = true;
boolean b2 = true;
boolean b3 = false;
System.out.println(b1^b2&b3);//1
}
}
Output: true.
So b1^b2&b3 can not be equivalent to (b1^b2)&b3 = false&false = false
(you can test it by changing line 1 : add convenient parenthesis)
Instead, it clearly looks to be similar to b1^(b2&b3) = true^(false) = true
Cyril.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I am not sure what the question is.
The precedence of the bitwise and logical operators is
&
^
|
They do not have equal precedence. They do not group from left to right.
Precedence rules specify that certain operators, in the absence of parentheses, group "more tightly" than other operators.
Since operator precedence is the "stickiness" of operators relative to each other,
(b1|b2&b3^b4) is equivalent to (b1 | ((b2 & b3) ^ b4))
[ August 03, 2003: Message edited by: Marlene Miller ]
Ben Ethridge
Ranch Hand

Joined: Jul 28, 2003
Posts: 108
Interesting, Marlene. Nothing is in any of my java books (Thinking in Java and the Heller/Roberts Study Guide)about that deeper level of "stickiness". Thanks. Where are you getting your info?
Ben
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Both Roberts/Heller and Eckel do talk about this, though perhaps not enough to make it clear for a newbie. Look for the sections that talk about "precedence", right where they first mention operators. Or see this in Sun's tutorial:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
Precedence refers to the order in which operators are grouped - it determines whether
a + b * c
should be viewed as
(a + b) * c
or
a + (b * c)
(it's the latter). However, once you've made those decisions, each individual operator will still be evaluated from left to right. (a, then b, then c.) Precedence doesnt' change that - but it governs what you do with the results. (After evaluating a, b, and c, do you add a to b and then multiply by c, or do you multiply b and c and then add to a?) Here, it may not matter what order you evaluate a, b, and c. But if they were method calls, or had other side effects like a ++ operator, then the order of evaluation would matter.
Hope that helps...


"I'm not back." - Bill Harding, Twister
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Charles.
Where are you getting your info?

The Java Language Specification - for a correct description of the language
The Java Programming Language - for understanding and insights.
The way the JLS Section 15.22 Bitwise and Logical Operators specifies operator precedence is a little strange.
The Java Programming Language Section 6.10 lists the operators on separate lines according to precedence. I learned the "stickiness" metaphor from them.
Marlene
[ August 03, 2003: Message edited by: Marlene Miller ]
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
OK, thanks Jim and Marlene for your responses.
So, what we can say:
1�) operator precedence has nothing to do with evaluation order
2�) & ^ | have not the same precedence
3�) as I guessed it, explanations of the concerned Mock's exam are also false.
Thanks again,
Cyril.
Ben Ethridge
Ranch Hand

Joined: Jul 28, 2003
Posts: 108
Thanks, all.
Yes, Jim. You are right. I went back and looked again. It's in there, but just not explained well with good examples. However, it is explained well in that tutorial. That's a nice tutorial. (Maybe I should have just studied that, instead of buying all these books for the exam. :-)
By the way, that tutorial also pretty fully explains my other question on this forum, regarding the meaning of the word "expression". (See "Expression/Operator Question")...
and "statement"...
and "operator", for that matter. Once you understand the definitions fully, you can apply them and the logic then becomes pretty clear.
Ben
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
operator precedence has nothing to do with evaluation order

Yes, precedence tells us where to add parentheses. Then we have to apply the rule: evaluate the left operand before the right operand.


It turns out, the operands are always evaluated from left to right, but the operators are not always performed in order of precedence.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator precedence
 
Similar Threads
operator
bitwise operator precedence
complex boolean expressions without parentheses
Operator precedence
Booleans and Operator precedence