++ / --
Increases or decreases a variable by one.
Available since: Java 1.0
Syntax
x++ ++x x-- --xExamples
The postfix form returns the old value, the prefix form returns the new one:
void main() {
int a = 5;
int b = a++;
IO.println(a + " " + b);
int c = 5;
int d = ++c;
IO.println(c + " " + d);
}Output:
6 5
6 6Notes
As a standalone statement, i++; and ++i; do the same. The difference only shows inside an expression, so avoid mixing ++ with other operations on one line. It also works for char and double: 'a' becomes 'b' after ++.
See also
- += -= *= /= — Performs an operation and stores the result back in the same variable.
- for — A loop with a counter, or iteration over every element of an array or collection (for-each).
In the tutorial
- Operators — Java arithmetic operators, integer division and remainder, compound assignment, increment, comparison and logical operators.