gpt4 book ai didi

java - 如果后自增/自减优先于前自增/自减,那么为什么这个表达式的计算结果是这样的 :

转载 作者:太空宇宙 更新时间:2023-11-04 12:22:46 25 4
gpt4 key购买 nike

public class Test {
public static void main(String[] args) {
int x = 3;
int y = ++x * 5 * x--;
System.out.println("x is " + x);
System.out.println("y is " + y);
}
}

输出为:

x is 3
y is 80

但是按照后操作符优先于前操作符的规则,不应该是这样的吗:

y = ++x * 5 * 3 (x is 2)
y = 3 * 5 * 3 (x is 3)
y = 45

相反,这段代码的行为就好像它只是从左到右计算表达式,在后递减之前计算前递增。为什么?

最佳答案

int y = ++x * 5 * x--;

++x => Increase then evaluate => x == 4
x-- => Evaluate then decrease

实际上它看起来像这样:

int y = 4 * 5 * 4; // == 80

并且由于递减运算符,您的 x 最后是 3

关于java - 如果后自增/自减优先于前自增/自减,那么为什么这个表达式的计算结果是这样的 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38660435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com