Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)[^]: The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.
result++;
++result;
result
++result
result++
int a = 5; int b = a++; // a = 6, b = 5 int c = ++a; // a = 7, c = 7
int a = 5; // int temp = a++; // is the same as int temp = a; a = a + 1; // Finally the value of the temp variable is assigned a = temp;
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)