x: 100 y: 100
When a variable is not part of a larger expression it makes no difference if you use the prefix operator or the postfix operator. For example:
count++ ; ++count ;
The following loop prints the values from 0 to 9, just as did the previous version (which used a postfix operator):
int counter = 0;
while ( counter < 10 )
{
System.out.println("counter is now " + counter );
++counter ;
}
It is wise to use the ++
operator
only with an isolated variable,
as above.
Using it in more complicated expressions is never necessary
and can lead to nasty bugs.
Java includes this only because it is familiar to C
programmers.
When Java was introduced C was the dominant programming language.
The language C includes the ++
and --
operators this because it was familiar
to assembly language programmers of the PDP-8 computer,
a common minicomputer of the 1970s.
The PDP-8 machine language included such operations.
Our 21st century programming language Java inherits some of its features from the architecture of an ancient computer!
Is subtracting one from a variable a common operation?