File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operator precedence

 
Win jones
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
&& 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
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before any operators operate, complete evaluation of operands takes place from left to right.
 
Win jones
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand, can u elaborate on complete evaluation of operands takes place from left to right
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic