Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0029 Increment and Decrement

++ and -- are Java's increment and decrement operators. The increment operator, ++, increases its operand by one. The decrement operator, --, decreases its operand by one. For example, this statement: x = x + 1; can be rewritten like this by use of the increment operator: x++; This statement: x = x - 1; is equivalent to x--; The increment and decrement operators are unique in that they can appear both in postfix form and prefix form. In the postfix form they follow the operand, for example, i++. In the prefix form, they precede the operand, for example, --i. The difference between these two forms appears when the increment and/or decrement operators are part of a larger expression. In the prefix form, the operand is incremented or decremented before the value is used in the expression. In postfix form, the value is used in the expression, and then the operand is modified.