gpt4 book ai didi

java - 当赋值运算符的优先级最低时,y=x++ 和 y=x-- 有何不同?

转载 作者:行者123 更新时间:2023-12-02 16:11:19 27 4
gpt4 key购买 nike

我是 Java 新手。刚发现在表达中,y=x++,y取x的值,x变为+1。如果我听起来很愚蠢,请原谅我,但根据 order of precedence ,赋值运算符在最后。所以 x++ 不应该首先发生然后是分配。提前致谢。

最佳答案

Q: So isn't x++ supposed to happen first followed by the assignment.

答:是的。这就是发生的事情。语句 y = x++; 等效于以下内容:

temp = x;      // | This is 'x++'
x = x + 1; // | (note that 'temp' contains the value of 'x++')

y = temp; // This is the assignment.

但是如您所见,操作顺序(++=)不会影响操作的实际作用。

因此……

Q: How are y=x++ and y=x-- different when the assignment operators has the least priority?

答:他们不是。

关于java - 当赋值运算符的优先级最低时,y=x++ 和 y=x-- 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67946625/

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