最佳答案
在 Java 中,我知道赋值计算为右操作数的值,所以像 x == (y = x)
这样的语句计算为 true
。
但是,此代码输出 false
。
public static void main(String[]args){
String x = "hello";
String y = "goodbye";
System.out.println(x.equals(x = y));
}
为什么会这样?在我的理解中,它首先计算 (x = y)
,它将 y
的值赋给 x
,然后返回 y
的值。然后计算 x.equals(y)
,它应该是 true
,因为 x
和 y
现在应该共享相同的引用,但是我得到的是 false
。
这里发生了什么?