gpt4 book ai didi

Java 加法与预增量根据括号的使用给出不同的结果

转载 作者:行者123 更新时间:2023-12-02 10:07:00 26 4
gpt4 key购买 nike

我在玩 Java 时遇到过这个。我想知道为什么这会给出 7.0 的结果:

float x = 3f;
int y = 4;
System.out.println(x+++y); // 7.0

而不是 8.0,就像我们使用括号时那样?

System.out.println(x+(++y)); // 8.0

最佳答案

您的第一个示例的计算结果为 x++ + y,但第二个示例的计算结果如预期的那样为 x+++y

x++ + y

Extract the value from x // x = 3
Extract the value from y // y = 4
Add them // sum = 7
Add 1 to x // x = 4

x+++y

Extract the value from x // x = 3
Add 1 to y // y = 5
Extract the value from y // y = 5
Add them // sum = 8

关于Java 加法与预增量根据括号的使用给出不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55282414/

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