Home

Increment and Decrement Operators in Java

|
|  Updated:  
2016-03-26 16:03:31
|   From The Book:  
No items found.
Java and XML For Dummies
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 is from the book: 

No items found.

About the book author:

Doug Lowe is the information technology director at Blair, Church & Flynn Consulting Engineers, a civil engineering firm. He has written more than 50 For Dummies books on topics ranging from Java to electronics to PowerPoint.