Theory:Increment and decrement
In this topic, we will discuss one of the most famous operations in programming: increment. It is used in many programming languages including Java to increase a variable by one. Fun fact: the name of C++ programming language is a reference to this operation, signifying the evolutionary nature of the changes from C.
# Using ++ and -- in Java
Java has two opposite operations called increment (++
) and decrement (--
) to increase/decrease the value of a variable by one.
int n = 10;
n++; // 11
n--; // 10
2
3
The code above is actually the same as below.
int n = 10;
n += 1; // 11
n -= 1; // 10
2
3
# Prefix and postfix forms
Both increment and decrement operators have two forms which are very important when using the result in the current statement:
- prefix (
++n
or--n
) increases/decreases the value of a variable before it is used; - postfix (
n++
orn--
) increases/decreases the value of a variable after it is used.
The following examples demonstrate both forms of increment.
Prefix increment:
int a = 4;
int b = ++a;
System.out.println(a); // 5
System.out.println(b); // 5
2
3
4
5
In this case, the value of a
has been incremented and then assigned to b
. So, b
is 5.
Postfix increment:
int a = 4;
int b = a++;
System.out.println(a); // 5
System.out.println(b); // 4
2
3
4
5
In Java, the postfix operator has higher precedence than the assignment operator. However, it returns the original value of a
, not the incremented one. That's why when we assign a++
to b
, we actually assign 4, and then variable a
is incremented. So, b
is 4 and a
is 5.
If that's still not clear enough for you, take a look at the code:
int a = 4;
System.out.println(a++ + a); // this is 9
2
We hope that now you fully understand increment and decrement and their prefix and postfix forms.