output of expression in (--i + ++i) in java

半腔热情 提交于 2019-12-07 14:12:02

问题


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 ?


回答1:


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?

  • Java Operator Precedence
  • Good article on associativity and precedence



回答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 



回答3:


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.



回答4:


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




回答5:


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.




回答6:


(--i + ++i)

Evaluation starts from left to right.

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

8 + 9 = 17



回答7:


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.




回答8:


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


来源:https://stackoverflow.com/questions/10812779/output-of-expression-in-i-i-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!