2

What does this do?

int x = 1;
int y = 2;
int z = 3;
x = y = z;

I have come across multiple of this type of thing in a few open source projects and have always been confused by them. By them I mean the = operator being used twice on three integers i.e. x = y = z;. I need to understand this. Thanks!

8 Answers8

11

The operator = evaluates the right hand side and assigns the result to the variable on the left hand side. The expression returns this value.

For this to work, evaluation proceeds from right to left:

x = (y = z);

y becomes 3 and the value returned is 3. Thus, x is assigned 3 as well.

laune
  • 31,114
  • 3
  • 29
  • 42
1

= is evaluating right to left

x = y = z will make x and y equal to z, i.e = 3

1

In most of the language = stands for assignment operator and the rule is lefthand side of assignment operator must be variable/object. (You can also do it like this object1=object2 but object2's reference will be the value here )

You can't do 2=3 or 2=2 what happening here is you are first assgning value of z to y

x=y=z

  1. y=z
  2. x=y

Here it's evaluated from right to left so y takes value of z and than x takes value of y

Probably here want to note you can't do x=4=5 directly.

It must be variable=variable=(value/variable)<------------------
  • So during this your z must have been initialized
akash
  • 22,664
  • 11
  • 59
  • 87
1

= operator will assign value of right side expression to the variable on left side.
So in x=y=z,first value of z will be assigned to y and then it is assigned to x.

STEP 1 : x=(y=z) /assign value of z to y.
STEP 2 : x=y /assign value of y to x.

So value of x will be 3.

Rohan
  • 3,068
  • 1
  • 20
  • 26
1

The assignment operator always evaluate right side expression so your statement equivalent to

//this expression is equal to 
x=y=z;
        y=z;
        x=y;
//so first y becomes 3 and the value assign to x
//means first assign the value of z to y than assign the value of y to x
MSR
  • 535
  • 7
  • 19
1

x = y = z is same as x=(y=z)

so, first y is assigned the value of z , i,e 3 and then the result is assigned to x.

At the end of it, values will be x = 3, y = 3 and z = 3

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
vinayknl
  • 1,242
  • 8
  • 18
1

The = operator evaluated from right to left.

So, when you write x=y=z it works form right hand side ti left hand side as

1. y=z i.e value of z assign to y

2. then x=y i.e new value of y now assign to x

for example,

if x=1,y=2 and z=3 then after x=y=z value of x=3,y=3 and z=3

value assign this way

x=y=z <-------------

rachana
  • 3,344
  • 7
  • 30
  • 49
0

Please read some specifications:

There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=(b=c), which assigns the value of c to b and then assigns the value of b to a.

See also:

  1. http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26
  2. http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html
  3. https://stackoverflow.com/a/12850755/1834700
Community
  • 1
  • 1
Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53