Search code examples
javac++unary-operator

Unary operators in java vs c++


Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
Is there any difference between the Java and C++ operators?

Why unary operators give different result in c++ and java?

Check this out:

int i = 1;
i = i++ + ++i;
print i  (with cout or println)

In java: prints 4

In c++: prints 5

Why ?


Solution

  • In C++ the behavior of a statement such as i = i++ + ++i; is actually undefined so the fact that the behavior differs is not so surprising.

    In fact it shouldn't be surprising if two different C++-compilers produce different behavior for the C++ statement i = i++ + ++i;.

    Related question: