Java原始变量在表达式期间的重新赋值

huangapple go评论78阅读模式
英文:

Java primitive variable reassignment during expression

问题

我在测试中遇到了一个让我感到困惑的问题(以下附有代码)。简单说,我认为变量被重新赋值,然后作为值添加回表达式中(使输出为"8, 10"),但似乎原始值在某种程度上并未更改。我漏掉了什么?

附言:如果存在类似的问题,我很抱歉,我找不到类似的问题(可能太明显了:P)。

class InitTest{
    public static void main(String[] args){
        int a = 10;
        int b = 20;
        a += (a = 4);
        b = b + (b = 5);
        System.out.println(a + ", " + b);
    }
}
英文:

I had a question in my test that I got confused about (code attached below). To put it shortly, I thought that the variables are reassigned and then added back as a value to the expression (making the output "8, 10") but seems like the original value somehow is not changed. What am I missing?

p.s. Sorry if a similar question exists, I couldn't find one (probably its too obvious :P).

class InitTest{
	public static void main(String[] args){
		int a = 10;
		int b = 20;
		a += (a = 4);
		b = b + (b = 5);
		System.out.println(a + ",  " + b);
	}
}

答案1

得分: 1

a += (a = 4);

上述代码在逻辑上等同于以下内容

a = a + (a = 4);

如果我们用现有的变量值替换 `a`,则简化为

a = 10 + 4 = 14

我们可以对变量 `b` 做同样的操作

b = b + (b = 5) = 20 + 5 = 25

我们得到这个结果是因为运算符优先级加法运算符 `+` 的优先级高于赋值运算符 `=`,这是由 Java [运算符优先级表](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) 定义的

可以看出加法赋值运算符 `+=` 与赋值运算符共享相同的优先级在这种情况下表达式从左到右进行评估

---

相反如果表达式是

a = (a = 4) + a;
b = (b = 5) + b;

那么它将得到您期望的输出(`a = 8`,`b = 10`),因为在计算表达式时会先计算左操作数然后再计算右操作数我会尝试查找这在 Java 语言规范中的具体规定位置
英文:
a += (a = 4);

The above is logically equivalent to the following:

a = a + (a = 4);

If we substitute in the existing value for a, then this simplifies to:

a = 10 + 4 = 14

We can do the same for b:

b = b + (b = 5) = 20 + 5 = 25

We see this result because of operator precedence. The addition operator, +, has a higher precedence than the assignment operator, =, as defined by the Java operator precedence table.

You can see that the addition-assignment operator, +=, shares the same precedence with the assignment operator, in which case the expression is evaluated from left to right.


If, instead, the expressions were:

a = (a = 4) + a;
b = (b = 5) + b;

Then it would result in the output that you expect (a = 8, b = 10), as the left operand is computed before the right operand (when evaluating the expression). I'll try to locate where this is specified in the Java Language Specification.

答案2

得分: -1

在Java中,当将一个值赋给一个变量时,首先计算的是等号(=)左边的部分,然后是右边的部分。因此,当你写下 a += (a = 4);,这等同于 a = a + (a=4);,这与 a = a + 4; 相同,对于变量b也是相同的。

英文:

In Java when assigning a value to a variable, the first thing that is calculated is the left part of the = sign and then the right. Therefore, when you write a += (a = 4); which is equivalant to a = a + (a=4) which is the same as a = a + 4 , same for b.

huangapple
  • 本文由 发表于 2020年9月2日 08:05:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63696958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定