java - how does pre - increments and post increments work? -
suppose have initialised 2 variables this
int a=0; int b=0;
now if assign b value
b=a++ + ++a + ++a;
now a=3 , b=5 shouldnt have been b=2 ? why b assigned value 5 ?
lets see:
a++ = 0, afterwards increased 1.
++a = (the 1 a++ plus 1 preincreased) 2
++a = (the 2 ++a above plus 1 preincreased) 3
in total: 0 + 2 + 3 = 5
this explains why three. in last step increased three.
Comments
Post a Comment