Win a copy of Design for the Mind this week in the Design forum!

# Order of Operators' Evaluation

Yalvin Duha
Ranch Hand
Posts: 41
I'm having a hard time deciphering how the expression is evaluated when various operator groups are combined (can't figure out the associativity), for instance:

In above code, a is output as "7" and b as "4"! How do we get here? How come the value of "b" is all of sudden altered to "4" (5 - 1 but how)? However, the following modification in order of operators -- from "+=--" to "+=+-") would yield a different result:

Which makes sense because I believe the expression above is expanded to "a = 3 + +(-5) = -2", but the former just doesn't make much sense.

I know the associativity of "+" and "-" works from left-to-right and "+=" from right-to-left.

saloni jhanwar
Ranch Hand
Posts: 583
Yalvin Duha wrote:I'm having a hard time deciphering how the expression is evaluated when various operator groups are combined (can't figure out the associativity), for instance:

In above code, a is output as "7" and b as "4"! How do we get here? How come the value of "b" is all of sudden altered to "4" (5 - 1 but how)?

In above scenario assignment operator(+=) has lower precedence than decrement operator(--).

Yalvin Duha wrote:
However, the following modification in order of operators -- from "+=--" to "+=+-") would yield a different result:

Which makes sense because I believe the expression above is expanded to "a = 3 + +(-5) = -2", but the former just doesn't make much sense.

I know the associativity of "+" and "-" works from left-to-right and "+=" from right-to-left.

And in this scenario also assignment operator(+=) has lower precedence than additive operator(+/-). There is no such operator like +- or -+ ,so it's just to make confuse,so you can think code like this

Yalvin Duha
Ranch Hand
Posts: 41
saloni jhanwar wrote:
Yalvin Duha wrote:I'm having a hard time deciphering how the expression is evaluated when various operator groups are combined (can't figure out the associativity), for instance:

In above code, a is output as "7" and b as "4"! How do we get here? How come the value of "b" is all of sudden altered to "4" (5 - 1 but how)?

In above scenario assignment operator(+=) has lower precedence than decrement operator(--).

You are right. I even mentioned it that the precedence works from right-to-left, and of course the unary operators are evaluated prior to assignment operators. I got thrown off by "+-" or "-+", eclipsing the fact that they are indeed unary operators ("--" and "++") and not additives, i.e. "- and -" or "+ and +". On second thought, I don't believe these precedence questions are in the test, right?

Thank you.

saloni jhanwar
Ranch Hand
Posts: 583
Yalvin Duha wrote:
You are right. I even mentioned it that the precedence works from right-to-left, and of course the unary operators are evaluated prior to assignment operators. I got thrown off by "+-" or "-+", eclipsing the fact that they are indeed unary operators ("--" and "++") and not additives, i.e. "- and -" or "+ and +". On second thought, I don't believe these precedence questions are in the test, right?

Thank you.

Yes they're unary operators here,and precedence questions are not important from test point of view.

Henry Wong
author
Marshal
Posts: 20995
76
• 1
Subject of this topic wrote:Order of Operators' Evaluation

Yalvin Duha wrote:I'm having a hard time deciphering how the expression is evaluated when various operator groups are combined (can't figure out the associativity), for instance:

Which makes sense because I believe the expression above is expanded to "a = 3 + +(-5) = -2", but the former just doesn't make much sense.

I know the associativity of "[b]+" and "-" works from left-to-right and "+=" from right-to-left[/b].

saloni jhanwar wrote:
In above scenario assignment operator(+=) has lower precedence than decrement operator(--).

This topic seems to mix "precedence", "associativity", and "order of evaluation".... I understand that this topic needs to address all three, but you need to understand that it is actually "three" -- meaning that precedence and associativity tables isn't enough.

To deal with order of evaluation, you need to understand section 15.7 of the Java Language Specification...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

Henry

Yalvin Duha
Ranch Hand
Posts: 41
Henry Wong wrote:

This topic seems to mix "precedence", "associativity", and "order of evaluation".... I understand that this topic needs to address all three, but you need to understand that it is actually "three" -- meaning that precedence and associativity tables isn't enough.

To deal with order of evaluation, you need to understand section 15.7 of the Java Language Specification...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

As always, you point to the right direction. Very helpful.

Thank you.

Ilakya Mukunth
Ranch Hand
Posts: 57
Check out the code below:

int m1(int i) {System.out.println(i + ","); return i;}

void test9()
{
int i = 0;
i = i++ + m1(i); System.out.println(i);
}
Output is:
1,
1

I check the operator precedence in this website:
http://bmanolov.free.fr/javaoperators.php

I expected the output to be 0,1.
does i++ evaluated before calling method m1()?
Can anyone help?