Yes, sometimes.
++counter means increment the value before using it. |
counter++ means increment the value after using it. |
The increment operator ++
can be put in front of a
variable.
When this is done,
it is a prefix operator.
When it is put behind a variable
it is a postfix operator.
Both ways increment the variable.
However: The prefix operator increments before the value in the variable is used.
int sum = 0;
int counter = 10;
sum = ++counter ;
System.out.println("sum: "+ sum " +
counter: " + counter );
This program fragment will print:
sum: 11 counter: 11
The ++ operator in this example is a prefix operator.
The value in counter is incremented before
the value is used.
The assignment statement is executed in two steps:
counter is incremented before use.
counter is now 11counter to sum
The next statement writes out: sum: 11 counter: 11
Inspect the following code:
int x = 99;
int y = 10;
y = ++x ; // prefix increment operator
System.out.println("x: " + x + " y: " + y );
What does this fragment write out?