Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Increment and Decrement Operators in Java

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
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.