Java Decrement Operator
Java Decrement operator takes single operand as input and decrements the value of operand by 1..
--
symbol is used for Decrement Operator.
Syntax – Decrement Operator
Following is the syntax for Decrement Operator.
--operand
//or
operand--
The first form of decrement is called pre-decrement. The value of the operand in returned and then the decrement happens.
The first form of decrement is called post-decrement. The value of the operand is decremented and then the value is returned.
The operand could be of any numeric datatype.
Example 1 – Decrement Integer
In the following example, we shall take an integer and decrement its value by one using Decrement operator.
Java Program
public class DecrementExample {
public static void main(String[] args) {
int a = 7;
a--;
System.out.print(a);
}
}
Output
6
Let us use pre-decrement form, and decrement the value.
Java Program
public class DecrementExample {
public static void main(String[] args) {
int a = 7;
System.out.println(--a);
System.out.print(a);
}
}
Output
6
6
Example 2 – Decrement Floating Point Value
In the following program, we shall decrement a float.
Java Program
public class DecrementExample {
public static void main(String[] args) {
float a = 7.2f;
a--;
System.out.print(a);
}
}
Output
6.2
Conclusion
In this Java Tutorial, we learned how to use Java Decrement Operator.