Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# When is an expression evaluated left to right AND when right to left?

Ranch Hand
Posts: 198
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?

author
Sheriff
Posts: 23507
138

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

Sheriff
Posts: 21328
87

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.

Ranch Hand
Posts: 488
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
Teach me to begin replying then go do something else.

lol

Greenhorn
Posts: 20
What does != mean? Don't assign?

Henry Wong
author
Sheriff
Posts: 23507
138

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

Marshal
Posts: 59092
180
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: 21328
87
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: 59092
180
Agree about side-effects, but did you mean "omit the a= part", Rob?

Rob Spoor
Sheriff
Posts: 21328
87
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.

 Squanch that. And squanch this tiny ad: Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss