Assignment operator chain understanding

JavaLearner

Can some one explain the output of below code

int a=10;
a = a -= a+= a -= a += a;

output : 10 

I am not able to get how it is giving 10?

jae

a += a means a = a + a.

likewise, a -= a means a = a - a.

I am not sure which way is proper to start, but if I convert the given code from the right using above,

   a += a                     >     a = a + a;
   a -= a += a                >     a = a - (a + a);
   a+= a -= a += a            >     a = a + (a - (a + a ));
   a -= a+= a -= a += a       >     a = a - (a + (a - (a + a)));
   a = a -= a+= a -= a += a   >     a = a - a - a + a + a;

where -a -a + a + a cancels each other, resulting a = a, which is 10.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related