+ 2

Increment and Decrement operators in Java and C++?

For this particular question. int x=0; cout<<(++x + --x) Gives '0' as the answer. in C++. Whereas in Java.. System.out.println (++x + --x) Gives '1' as the answer. Why?

29th May 2017, 5:56 PM
Victor Das
Victor Das - avatar
12 ответов
+ 6
@Victor In C++ if two or more operators with same precedence appears in an expression in a same line then the order of evaluation of operands is undefined. The result you may get is totally dependent on compiler specific implementation. In the above C++ case ++x + --x; while trying to evaluate the operands (hence expression), the compiler may choose to evaluate ++x first and then --x or it may choose to evaluate --x first and then ++x or it may choose to do something else altogether. Hence the result is either undefined or compiler dependent. There are tons of C++ compilers available and I am sure that the results will not be consistent across all of them.
30th May 2017, 6:07 AM
NeutronStar
NeutronStar - avatar
+ 6
@Abhishek, I didn't get about C++? There is no postfix operator used!
30th May 2017, 3:51 AM
Sachin Artani
Sachin Artani - avatar
+ 5
multiple usage of increment operators on a single line and a single variable will result in undefined behaviour. basically it is unknown as to which order the compiler will chose to complete the expression.
29th May 2017, 7:04 PM
jay
jay - avatar
+ 3
@Abhishek, You mean, x=0; ++x means 1, and then --x means -1 (which must be 0), and then 1 and -1 (which supposed to be 0) will cancel out and output 0? I'm satisfied with NeutronStar
31st May 2017, 7:27 AM
Sachin Artani
Sachin Artani - avatar
+ 2
that's y all prefix forms are evaluated first.. first increment increases x by 1 but then decrement nullifies the effect and then the final result is put into expression which is 0+0
30th May 2017, 3:54 AM
Abhishek Mehandiratta
Abhishek Mehandiratta - avatar
+ 2
Nullifies? Java does not nullifies but simply decrements by 1, when use --x; Why C++ nullifies?
30th May 2017, 3:59 AM
Sachin Artani
Sachin Artani - avatar
+ 1
in java if an expression doesn't have parenthesis then it is always evaluated from left to right keeping in mind the operator precedence of operators encountered. here ++x is encountered first so it results in 1 and then --x results in 0 hence addition is 1... in c++ precedence of prefix operators is always greater than postfix versions.. hence ++x results in 1 but --x results in 0 so ultimately 0 will be kept for x in expression
30th May 2017, 3:44 AM
Abhishek Mehandiratta
Abhishek Mehandiratta - avatar
+ 1
@Victor, Thank you for marking my answer as best : ) Sorry it was due for quite sometime.
17th Jun 2017, 12:00 PM
NeutronStar
NeutronStar - avatar
0
@Sachin by nullify I mean that both postfix and prefix versions occur one after the other hence there's No effect
31st May 2017, 2:12 AM
Abhishek Mehandiratta
Abhishek Mehandiratta - avatar
0
@sachin also if u read operator precedence of java then prefix and postfix have same precedence and in such an expression evaluation always takes from left to right
31st May 2017, 4:16 AM
Abhishek Mehandiratta
Abhishek Mehandiratta - avatar
0
here the evaluation is 0 + 0 in c++ which results in zero. if you consider this code int a=0; cout<<(++a + ++a +a++); it will print 6 remember in c++, the evaluation is from right to left. here the evaluation is 3+3+0 that is pre-increment takes the final value and post-increment takes the value at that instant and then increments it. but in Java, the answer is 5. Here in Java, the evaluation is done from left to right here the evaluation is 1+2+2 that is pre-increment first increments the value and then takes the value and post-increment first takes the value and then increments it. the main difference in java and c++ 1)direction in which the evaluation is done. 2)pre-increment and pre-decrement operators.In c++, PI and PD take the final value of the variable and in Java, PI and PD take the value at that instant.
16th Apr 2018, 6:49 AM
pavan
pavan - avatar
- 1
@Abhishek In C++ postfix operators have higher precedence than prefix operators.
30th May 2017, 5:49 AM
NeutronStar
NeutronStar - avatar