package com;
public class Test {
public static void main(String[] args)
{
int a= 11, b = 10;
a = a++ + ++b; //why? output is "22 11" and not "23 11"
System.out.println(a+" "+b);
}
}
最佳答案
以下是表达式的求值方式(粗略地说,我没有检查 JLS 的求值顺序,但我认为是从左到右):
a = a++ + ++b; // a is 11, b is 10
a = 11 + ++b; // a is 12 but its previous value 11 was returned by a++, b is 10
a = 11 + 11; // a is 12, b is 11 and its updated value was returned by ++b
a = 22; // a is 12, b is 11 and its updated value was returned by ++b
因此,这是预期的结果,您只需应用这些运算符的定义即可。
关于java - 为什么在此代码中后递增运算符无法递增 'a'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57936422/