Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon

Increment (++) and decrement (--) operators in Java programming let you easily add 1 to, or subtract 1 from, a variable. For example, using increment operators, you can add 1 to a variable named a like this:

a++;

An expression that uses an increment or decrement operator is a statement itself. That’s because the increment or decrement operator is also a type of assignment operator because it changes the value of the variable it applies to.

You can also use an increment or decrement operator in an assignment statement:

int a = 5;
int b = a--;    // both a and b are set to 4

Increment and decrement operators can be placed before (prefix) or after (postfix) the variable they apply to. If you place an increment or decrement operator before its variable, the operator is applied before the rest of the expression is evaluated. If you place the operator after the variable, the operator is applied after the expression is evaluated.

For example:

int a = 5;
int b = 3;
int c = a * b++;   // c is set to 15
int d = a * ++b;   // d is set to 20

About This Article

This article can be found in the category: