+ 1

What happened here...say int b=3; int a=16; b+=++a; system.out.println(b);

how did the answer came to be....

18th Sep 2017, 12:19 PM
Ogbonna Christian
Ogbonna Christian - avatar
3 Answers
+ 8
b += ++a; b += 4; // pre-increment b = b + 4; b = 20
18th Sep 2017, 2:11 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 3
@Mihail The original value is not returned because this is the pre-increment (also called pre-fix) ++x first increments x then uses x. So, b becomes 4, then 4 is returned. 16 + 4 = 20.
18th Sep 2017, 4:04 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
it should be 19 because ++a adds one but returns it's original value and 16+3=19
18th Sep 2017, 12:28 PM
Mihail
Mihail - avatar