Java: pre-,postfix operator precedences

前端 未结 5 667
-上瘾入骨i
-上瘾入骨i 2020-12-03 14:41

I have two similar questions about operator precedences in Java.

First one:

int X = 10;
System.out.println(X++ * ++X * X++); //it pr         


        
5条回答
  •  鱼传尺愫
    2020-12-03 15:07

    The reason why its 1440 is because

    1. x is set to 10 i.e 1st term is FIXED(overall equation 10 *)

    2. x is incremented by 1,x =11 now

    3. x is pre-incremented by 1 x=12 and second term FIXED now (overall equation 10 * 12 *)

    4. now x is set to 12 and third term FIXED(overall equation 10 * 12 *12)

    5. x is increment now but is in this case not used for evaluation,

    in short a term is FIXED when variable occurs which in this case is X

    2nd case: I'm not sure but I guess can be broken as,

    1. a=b+a
    2. a++

    which I think is what is happening.

提交回复
热议问题