This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Help: Oeperator Associativity 2?

Ruff Young
Ranch Hand
Posts: 513
The question below is at operators in Dan's Topic exam. The output is 1. The question looks so tricky. How do I group it? And could you give me some explanation of it in detail?
Each time execute part caculation, the variable 'i' is being changed? I think so.
Some my approch,
a compound assignment expression of the form E1 op= E2 can be rewritten as E1=(T)((E1)op(E2)) where T is the type of E1.
so i = i + (~i - -i * ++i + i-- % ++i * i++)
then i = 1 + (~i - -i * ++i + i-- % ++i * i++)
...
class F {
public static void main (String []s) {
int i = 1;
i += ~i - -i * ++i + i-- % ++i * i++;
System.out.print(i);
}
}

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
With the following information you might be able to find out the solution:
Operator precedence
How does the ++ operator work?

Barry Gaunt
Ranch Hand
Posts: 7729
This may help:My analysis of some expressions.

Dan Chisholm
Ranch Hand
Posts: 1865
The following class provides a sequence of simplified versions of the expression. Each print statement prints the same result.

I hope the above helps.

Ruff Young
Ranch Hand
Posts: 513
I really appreciate you explain kindly. I am planning to take the exam in next week.