简体   繁体   English

Java中的赋值运算符

[英]Assignment Operators in java

We can write the same statement in two different ways as follows. 我们可以按以下两种不同方式编写相同的语句。 I have a question with providing two outputs for the value of x as follows. 我有一个问题,为x的值提供两个输出,如下所示。

int x = 10;
x = x*2+5; // Here the value of x is 25.

x = 10;
x *= 2+5; // Here the value of x is 70.

It is clear that this is because, 1. In the first statemnt x is multiplied by 2 then add 5. 2. In the second statemnt add 5 to 2 together then multiplied by x. 很明显,这是因为:1.在第一个状态中,x乘以2,然后加5。2.在第二个状态中,将5加到2,然后乘以x。 But why is it acting like this? 但是为什么会这样呢?

Because your sec statement will be evaluated as x = x * (2+5); 因为您的sec语句将被评估为x = x * (2+5);

x = 10;
x *= 2+5;
x = x * (2+5);

While in the first case, its normal left to right precedence.Java guarantees that all operands of an operator are fully evaluated before the operator is applied. 在第一种情况下,它通常具有从左到右的优先级。Java保证在应用运算符之前,必须对运算符的所有操作数进行全面评估。

A compound assignment operator has the following syntax: 复合赋值运算符具有以下语法:

<variable> <op>= <expression>

and the following semantics: 以及以下语义:

<variable> = (<type>) (<variable> <op> (<expression>))

The type of the "variable" is "type", and the "variable" is evaluated only once. “变量”的类型为“类型”,并且“变量”仅被评估一次。 Note the cast and the parentheses implied in the semantics. 请注意语义中隐含的强制转换和括号。 Here "op" can be any of the compound assignment operators(*,%, / etc). 这里的“ op”可以是任何复合赋值运算符(*,%,/等)。 The compound assignment operators have the lowest precedence of all the operators in Java, allowing the expression on the right-hand side to be evaluated before the assignment. 在Java中,复合赋值运算符的优先级最低,因此可以在赋值之前对右侧的表达式求值。

See: Operator precedence in Java . 请参阅: Java中的运算符优先级 * binds tighter than + which both are tighter than = or *= . *绑定比+更紧密, +都比=*=更紧密。

In your first statement x=x*2+5 it gives x=25 because * has higher priority compare to +. 在第一个语句x = x * 2 + 5中,它给出x = 25,因为*与+相比具有更高的优先级。 so It evaulates like 所以它像

x=10*2
x=20+5;

Because your sec statement will be evaluated as x = x * (2+5); 因为您的sec语句将被评估为x = x *(2 + 5);

In your second statement you can see it has bracket.So openinng bracket ( has higer priority compare to *.So it first calculate the bracket data and then it multiply with x. 在第二个语句中,您可以看到它带有方括号。因此,openinng方括号(与*相比具有更高的优先级。

X=x*(2+5)
x=10*7;
int x = 10;
x = x*2+5;



* has more precedence than + . So 10 * 2 + 5 = 25


x = 10;
x *= 2+5; 



+ have more precedence than *=. So the result is 70

For the case 2 The operator '=' has lowest priory so operand 2 + 5 evaluated then operator '*=' evaluated (because operator = has lowest priory than '+'). 对于情况2,运算符'='的优先级最低,因此对操作数2 + 5进行评估,然后对运算符'* ='进行评估(因为运算符=的优先级最低于'+')。 Only in that time the operator ' ' come in to the scene. 只有那时操作员才能进入现场。 So 10 * 7 is assigned to X 因此,将10 * 7分配给X

This is an example of combining an arithmetic operator with the simple assignment operator to create compound assignments . 这是将算术运算符与简单赋值运算符组合以创建复合赋值的示例。 With a simple assignment operator, the value on the right of the operator is calculated and assigned to the operand on its left. 使用简单的赋值运算符,将计算该运算符右侧的值,并将其分配给其左侧的操作数。 For example: 例如:

int x = 2 + 5;

obviously gives a value for x of 7. x的值显然为7。

The compound assignment operator follows the same basic idea: the value on the right of the operator is calculated, modified by the compound assignment operator (in this case multiplying the existing value of x by the final calculated value 7, and assigned to the operand on the left. 复合赋值运算符遵循相同的基本思想:计算该运算符右侧的值,并由复合赋值运算符进行修改(在这种情况下,将x的现有值乘以最终的计算值7,然后将其赋给on左边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM