Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Decrement
Java Variables Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Variables Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java variables and operators.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
The decrement operator functions similarly to the increment operator. The only difference is that the decrement operator decreases a value by one. This means we can write varOne--; instead of writing varOne = varOne - 1;. Let's take a look at these decrementing variables in the code below.
DecrementExample.java
package exlcode;
public class DecrementExample {
public static int exampleVariableOne = 10;
public static void main(String[] args) {
// Both statements subtract 1 from exampleVariableOne
exampleVariableOne--;
--exampleVariableOne;
System.out.println(exampleVariableOne);
}
}
Like the increment operator, there are two different ways to use the decrement operator called prefix and postfix decrement. The prefix decrement looks like --variablename; while the postfix increment looks like variablename--;. Both of these subtract one to the value in the variable. --varOne; decreases the value of varOne by one before returning the value of varOne. On the other hand, varOne--; returns the original value of varOne before decreasing the value of varOne by one. It is unlikely that we will print these results, but it is essential to know the difference between the functionality as it is commonly used in loops. Additionally, these operators will reduce the amount of code the programmer has to write.