a += a++ * a++ * a++ in Java. How does it get evaluated?

前端 未结 6 586
旧时难觅i
旧时难觅i 2020-12-10 15:27

I came across this problem in this website, and tried it in Eclipse but couldn\'t understand how exactly they are evaluated.

    int x = 3, y = 7, z = 4;

          


        
6条回答
  •  轮回少年
    2020-12-10 16:04

    The postfix operator x++ means something like "give me the value of x now, but increment it for future references"

    So, by the order of operations and evaluation,

    x++ * x++ * x++

    is interpreted first as

    3 * 4 * 5 (=60)

    Which is then added to the original 3, yielding 63.

    The original value is used because it's on the same line, had you written something like:

    int x = 3;
    
    int y += x++ * x++ * x++; 
    x += y;
    

    x would now be 66, instead of 63 because the x in the second line is now 6, rather than its original 3.

提交回复
热议问题