gpt4 book ai didi

java - 递增运算符值(java)?

转载 作者:行者123 更新时间:2023-12-02 06:38:53 27 4
gpt4 key购买 nike

为什么下面的代码没有执行到5?

int i = 5;
System.out.println(i-- + (5 - i--));

输出:

6

由于 i-- 在语句执行后递减并且 (5 - i--(应该是 5)) = 0 那么该值不应该简单地等于 5 吗?我在这里缺少什么概念?

最佳答案

为了踢球

i--              + (5 - i--)
5 (i value is 4) + (5 - 4 (i value is 3))
5 + 1

i-- 后缀递减运算的结果是 -- 之前的变量值。然而,该变量在求值后仍然会递减。

来自Java Language Specification

The result of the postfix decrement expression is not a variable, but a value.

the value 1 is subtracted from the value of the variable and the difference is stored back into the variable.

关于java - 递增运算符值(java)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19350115/

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