Yes.
The increment operator ++
can be put in front of a
variable.
When it is put in front of a variable
(as in ++counter
) it is called a prefix operator.
When it is put behind a variable
(as in counter++
) it is called a postfix operator.
Both ways increment the variable.
However:
++counter | means increment before using. |
counter++ | means increment after using. |
When the increment operator is used as part of an arithmetic expression you must distinguish between prefix and postfix operators.
int sum = 0; int counter = 10; sum = ++counter ; System.out.println("sum: "+ sum " + counter: " + counter );
This fragment requires careful inspection:
counter
is incremented before
the value it holds is used.counter
is incremented before use.)sum: 11 counter: 11