This week's book giveaway is in the OCAJP 8 forum.We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!

Order of Operators' Evaluation

Yalvin Duha
Ranch Hand

Joined: Apr 07, 2012
Posts: 40

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

Joined: Feb 09, 2012
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

Tell the difficulties that i am difficult.
Yalvin Duha
Ranch Hand

Joined: Apr 07, 2012
Posts: 40

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

Joined: Feb 09, 2012
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
Sheriff

Joined: Sep 28, 2004
Posts: 20154

46

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

Joined: Apr 07, 2012
Posts: 40

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

Joined: Mar 13, 2012
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?

I agree. Here's the link: http://aspose.com/file-tools

subject: Order of Operators' Evaluation