• Post Reply Bookmark Topic Watch Topic
  • New Topic

When is an expression evaluated left to right AND when right to left?  RSS feed

 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always have this BIG confusion regarding whether any given expression will be evaluated from left to right OR right to left.

What are the rules regarding this?

Suppose I have:



How is the value of c decided?
 
Henry Wong
author
Sheriff
Posts: 23292
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It's a bit more complex than that. Operators has precedence *and* association. Higher precedence operators are done first, and if the precedence is the same, then you use the association to determine whether it goes from right to left, or left to right.

Google "java operator precedence" for a chart -- on the precedence and association.

Henry
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
(http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html)

So let's break this code down:
1) != goes before assignment:
boolean c = (b != a) = !b;

2) assignment goes from right to left:
(b != a) = !b first, then c = <result of previous>

3) you get a compiler error:
It is trying to assign the value of !b to (b != a), and that's not allowed.

One set of parentheses fixes this:
These parentheses ensure that the operator precedence is changed, and the assignment "a = !b" goes before the comparison with b.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
operators

That link should help clear some doubt. Every operator has a precedence. Operators with a higher precedence are evaluated first. This is important to know for the SCJP and other tests but when it comes to real life coding always use parenthesis.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Teach me to begin replying then go do something else.

lol
 
james dunster
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does != mean? Don't assign?
 
Henry Wong
author
Sheriff
Posts: 23292
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james dunster wrote:What does != mean? Don't assign?


It is a conditional operator. It returns true when the left doesn't equal the right.

Henry
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do those operator links actually tell us the association direction? Does the bit about evaluated left to right mean the same as "associates to the left?"

By the way: isn't
b != (a = !b)
a tautology anyway?
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will return true alright, but you can't forget the side effects. Still, it will be the same if you omit the "b !=" part.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree about side-effects, but did you mean "omit the a= part", Rob?
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, now I think about it more, you can't omit either. If you omit the "a =", true is returned but the assignment is dropped. If you omit the "b !=", the assignment is there but the result can be both true or false.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!