posted 11 years ago

Aaaaagh, my eyes! Note that anyone who writes code like this in the real world is in big trouble; you find this sort of silliness in SCJP mock questions, and nowhere else.

That said...

b also starts at zero. We add to it the result of evaluating (c++), which is just the value of c. Here, the "++" happens

And that's the output.

That said...

**int c = ++a + ++a;[b]**

This means c is (++a) + (++a). a starts at zero. The first expression in parentheses equals 1, and the second equals 2, so c becomes 3.

[b]b += c++;This means c is (++a) + (++a). a starts at zero. The first expression in parentheses equals 1, and the second equals 2, so c becomes 3.

[b]b += c++;

b also starts at zero. We add to it the result of evaluating (c++), which is just the value of c. Here, the "++" happens

*after*the value of c is read. So since c is 3, b also becomes 3.And that's the output.

posted 11 years ago

OR

by default integers are zero, So

int c = 0;

int b = 0;

c = (1) 1 + (2) 2 = 3

b = b + (c++) = 0 + 3 (4) = 3

So b = 3 and c = 3 just like Ernest said.

JC

by default integers are zero, So

int c = 0;

int b = 0;

c = (1) 1 + (2) 2 = 3

b = b + (c++) = 0 + 3 (4) = 3

So b = 3 and c = 3 just like Ernest said.

JC

Author and Instructor, my book

Suhita Reddy

Ranch Hand

Posts: 60

posted 11 years ago

All that been said the only differente between

Hence, when you are using += *= -= or /= operators be careful with precision loss. It is bug hard to find, and your compiler will not even complaint about it.

Regards,

Edwin Dalorzo

[ July 07, 2006: Message edited by: Edwin Dalorzo ]

*a=a+b*and*a+=b*is that += autocasts the result.Hence, when you are using += *= -= or /= operators be careful with precision loss. It is bug hard to find, and your compiler will not even complaint about it.

Regards,

Edwin Dalorzo

[ July 07, 2006: Message edited by: Edwin Dalorzo ]

Suhita Reddy

Ranch Hand

Posts: 60

It is sorta covered in the JavaRanch Style Guide. |