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

前端 未结 6 608
旧时难觅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 15:58

    Because a postincrement modifies the variable after the value is taken and += evaluates its left hand side before evaluating its right hand side,

    x += x++ * x++ * x++; 
    

    becomes

    tmp0 = x
    
    tmp1 = x
    ++x
    tmp2 = tmp1 * x
    ++x
    tmp3 = tmp2 * x
    ++x
    
    x = tmp0 + x
    

提交回复
热议问题