3
int i=9;
System.out.println(--i + ++i);

output on execution : 17
The final value of i is : 9

But according to associativity and precedence rules in java,, ++i should be executed first i.e from Right to left which gives 10 and then --i gives 9 .. adding both,, the answer should be 19... As far as i have known such a code gives undefined behaviour in C/C++ but in java ,, the rules are strictly defined and there is no concept of sequence points. So, can anyone clarify the problem as iam really confused about this ?? Also in some books it was mentioned that post increment and post decrement operators are LTR associative. But in some other books it's given all increment and decrement(both post and pre) are RTL associative..!! Can anyone give a correct operator precedence and associativity table for java ?

assylias
  • 321,522
  • 82
  • 660
  • 783
Dean Mathew
  • 31
  • 1
  • 2
  • possible duplicate of [++i + ++i + ++i in Java vs C](http://stackoverflow.com/questions/3879176/i-i-i-in-java-vs-c) and especially [this answer](http://stackoverflow.com/a/3879280/829571). – assylias May 30 '12 at 08:39

8 Answers8

10

Can you point to where in the Java Language Specification it says that associativity in right-to-left? It is not, it is left to right (with the exception of multiple assigments - e.g. x = y = 4). See JLS section 15.7.1, helpfully titled "Evaluate Left-Hand Operand First." Hence the answer is correct:

--i + ++i

First --i is evaluated. This decrements i (which is now 8) and returns the post-decrement value (8). This is then added to ++i which is equivalent to increment-and-get (so the RHS evaluates to 9).

It's similar to

AtomicInteger i = new AtomicInteger(9);
i.decrementAndGet() + i.incrementAndGet();

Would you expect this to be evaluated r-l also?

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • He probably confuses the associativity of infix + and pre-increment ++. This book (http://introcs.cs.princeton.edu/java/11precedence/) says that pre-increment ++ is right-to-left. – nhahtdh May 30 '12 at 08:49
  • Sorry ! i Didn't understand !! According to (http://introcs.cs.princeton.edu/java/11precedence/) the pre inc and pre dec operators have same precedence and higher than '+' . Therefore should be evaluated before + and from RTL(since RTL associative). – Dean Mathew May 30 '12 at 17:05
2

Expression evaluation goes from left to right

(--i + ++i)

--i = 8 //i becames 8 here
++i = 9 // i becames 9 here again
8+9 = 17 
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
1

first --i becomes 8 and then ++i becomes 9.so answr is 17.

int i =9;

--i =8, now i is 8
++i=9 ,++ makes 9

so finally (8+9)=17 and i=9.
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
1

Evaluation starts from left to right. Do --i which becomes 8 and ++i which evaluates to 9 and addition is done which gives 17.

kiecodes
  • 1,642
  • 14
  • 28
  • This has been answered and explained already, so what was your intention to do that again? – Tom Feb 05 '17 at 14:35
0

The output is correct:

First, --i is executed first, so i becomes 8 then ++i is executed. Now, i is 8 so ++i becomes 9, then the middle + which becomes 8 + 9 = 17.

Java does associativity from left-to-right.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

(--i + ++i)

Evaluation starts from left to right.

first    (
second --i   // 8
third    +   // addition operator
fourth ++i   // 9
fifth    )

8 + 9 = 17
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

int i=9; System.out.println(--i + ++i);

as we know that, precedence of increment and decrement is higher than arithmetic operator '+', so '++' and '--' is evaluated first. and now associativity of unary operator is right-to-left, so according to this it should evaluate '++' operator first. but before evaluation, there is another concept of binding so, it will bind expression as ((--i)+(++i)) and now according to () operator, whose associativity is left-to-right, it will evaluate first (--i) and then (8+ (++i)) and i=8. now it will evaluate (++i) then (8+9) and i=9, and so finally answer will be (8+9) = 17.

Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
Nitya
  • 1
-1

Expression evaluation goes from left to right

i=9;
First --i than i=9-1, i=8
After ++i than i=8+1, i=9
--i + ++i  = 17
Spammer
  • 1
  • 2
  • This has been answered and explained already, so what was your intention to do that again? – Tom Feb 05 '17 at 14:35