namalfernandolk
namalfernandolk

Reputation: 9134

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.

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. But why is it acting like this?

Upvotes: 0

Views: 867

Answers (5)

Suresh
Suresh

Reputation: 1504

In your first statement x=x*2+5 it gives x=25 because * has higher priority compare to +. so It evaulates like

x=10*2
x=20+5;

Because your sec statement will be evaluated as 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.

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

Upvotes: 1

happydude
happydude

Reputation: 3889

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.

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.

Upvotes: 0

nidhin
nidhin

Reputation: 6910

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 '+'). Only in that time the operator '' come in to the scene. So 10 * 7 is assigned to X

Upvotes: 0

Shashank Kadne
Shashank Kadne

Reputation: 8101

Because your sec statement will be evaluated as 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.

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). 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.

Upvotes: 5

perelman
perelman

Reputation: 1777

See: Operator precedence in Java. * binds tighter than + which both are tighter than = or *=.

Upvotes: 3

Related Questions