Search code examples
javapostfix-operator

Compiler warns "Value of increment value (bar++) is used" - does this mean the behavior changes?


When I compile the following piece of code, I got a warning from the compiler saying "Value of increment value (bar++) is used"

int foo = 1, bar = 2;
return foo + bar++; // foo and bar are ints

So that means the value returned would be 4, rather than 3, which is surprising because I was expecting 3

Is it universal to all java compiler? Or just the one I' using (NetBeans) that's doing something different?

Thanks


Solution

  • 1) All Java compilers will produce the same bytecode for your code and the result will be 3. Just test it.

    2) As for the warning, it means that return foo + bar++; is the same as return foo + bar;