有人能解释一下以下代码的实现吗?
int j = 1;
System.out.println(j-- + (++j / j++));我期望输出为3,如下所示:因为'/‘比'+’具有更高的优先级,所以首先对它进行评估。
op1 = ++j (Since it is pre-increment, the value is 2, and j is 2)
op2 = j++ (Since it is post-increment, 2 is assigned and j is incremented to 3)因此,parantheses中的“/”运算的结果是2/2= 1。然后是“+”操作:
op1 = j-- (Since it is Post-increment, the value is 3, and j is decremented to 2)
op2 = 1 (The result of the '/' was 1)所以,结果应该是3+1= 4。但是当我计算这个表达式时,我得到了2,为什么会发生这种情况呢?
发布于 2015-03-03 10:56:42
因为'/‘具有比'+’更高的优先级,所以首先对它进行计算。
不,表达式从左到右计算-然后使用优先级规则将每个操作数关联起来。
因此,您的代码相当于:
int temp1 = j--; //1
int temp2 = ++j; //1
int temp3 = j++; //1
int result = temp1 + (temp2 / temp3); //2https://stackoverflow.com/questions/28829957
复制相似问题