O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Incrementing and Decrementing Operators

This is the kind of thing you need to do with some frequency, especially within loops. Java makes incrementing and decrementing a variable by 1 easy with these special operators:

x = 0;
x++;
//x is 1

You can subtract the same way:

y = 10;
y--;

The preceding operators are called post-increment and post-decrement, because they add or subtract 1 after the variable has been evaluated.

FRIDGE

You may only use these operators with variables—not with literals. That is, you can't do this: 10++;. That's illegal! You can't do this either: String x -= “something”;. No funny stuff, now.

You can also use these operators ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required