Java Increment and Decrement Operators
In all programming languages there is a common requirement to add or subtract 1 from a variable, this can be required of part of the program loops a certain number of times or there is a need to count down. This is done efficiently using the increment ++ and decrement — operators, and we have 2 options for each:
Type | Example | Operation |
---|---|---|
Post-increment | i++ | Returns i, THEN increments i |
Pre-increment | ++i | Increments i, returns that value |
Post-decrement | i-- | Returns i, THEN decrements i |
Pre-decrement | --i | Decrements i, returns that value |