wood burning stoves 2.0*
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

Win jones
Ranch Hand

Joined: Dec 12, 2004
Posts: 38
Dan chisholm Mock exam

class EBH202 {
static boolean a, b, c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}}

Ans) Prints: true,false,false

Explanation given first a= true is evaluated and since this is true the other part is not evaluated my question is when is left to right and operator precedence evaluated.

I was under the impression that since && has higher precedence over || the expressions
(b = true) && (c = true) is evaluated first followed by (a = true), can someone explain to me when the operands and precedence are evaluated

Also
int i=0, j = ++i + ((++i * ++i) % ++i) + ++i; The output of j = 8 eval as 1 + ( (2 *3) % 4) + 5
Can someone give me an example which justifies the statement that the prefix unary operator associates from Right to Left?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
On the first part:

(a = true) || (b = true) && (c = true)

Because the || is a short circut operator the compilier is able to determine that whatever is after the || is irrelevant and goes no farther. If what is before the || is false it will the evaluate the && before it evaluates the ||
Win jones
Ranch Hand

Joined: Dec 12, 2004
Posts: 38
Thanks steve, I understand how || and && work - short -circuit part but I am unable to understand how the precedence of && and || applies to statements.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
&& has a higher precidence over || so the
b && c
would be evaluated first and its result would be used
a || result
If the b && c were needed to understand the outcome.
however in this case that precidence is never taken into acount.
The question here isn't a matter of operator precidence it is an understanding of the short circut operation of conditional statements.
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
Before any operators operate, complete evaluation of operands takes place from left to right.


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Win jones
Ranch Hand

Joined: Dec 12, 2004
Posts: 38
Thanks guys, can you provide me some sample code where the prefix unary operator is evaluated based on Right to left operator precedence.

thanks in advance
Win jones
Ranch Hand

Joined: Dec 12, 2004
Posts: 38
Anand, can u elaborate on complete evaluation of operands takes place from left to right
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The problem is that precedence and associativity are linear approximations of how a compiler analyzes an expression in two dimensions. You'll notice the the Java Language Specification authors have explicitly refused to include an operator precedence table.

Below is an expression and its abstract syntax tree. As you can see, because && has a higher precedence, it groups its operands more tightly. So a || b && c is equivalent to a || ( b && c ). That does not mean that ( b && c ) is evaluated first. The controlling order-of-evaluation rule in the JLS is that the left operands of binary operators are always fully evaluated before the right operands. Finally, the operator is evauated. Notice how differently
( b = true ) && ( c = true ) || ( a = true ) would come out.

There are plenty of good books and web resources dealing with abstract syntax trees, if you want to understand this further.


a || b && c



Mike Gershman
SCJP 1.4, SCWCD in process
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
can you provide me some sample code where the prefix unary operator is evaluated based on Right to left operator precedence.

Sure thing, pardner.



[ January 03, 2005: Message edited by: Mike Gershman ]
[ January 03, 2005: Message edited by: Mike Gershman ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Operator precedence
 
Similar Threads
Dan's Tests | Question from 'Conditional Operator'
boolean x = (a = true) || (b = true) && (c = true);
Precedence
operator precedence
Logical operators (&& and ||)