Java Decrement Operator
Program Description
Write a java program to demo the decrement operator. We will look at a simple java program using the decrement operator. We will have a look at the postfix-decrement operation and prefix-decrement operation in the program.
Syntax
– – Decrement operator: decrements the variable value by 1
Postfix decrement
b=a–;
Prefix decrement
b= –a;
Java Program
// DecrementOperatorDemo.java program
public class DecrementOperatorDemo {
public static void main(String[] args) {
int a = 4;
a--;
//print the variable
System.out.println("a after Decrement should print 3= " + a);
int b;
b= a--;// a is assigned to b and then decremented
//print the variable
System.out.println("b after post decrement should print 3= " + b);
// a value is 2 now
b= --a;// a value is decremented and assigned to b
//print the variable
System.out.println("b after pre decrement should print 1= " + b);
}
}
Program Output
a after Decrement should print 3= 3
b after post decrement should print 3= 3
b after pre decrement should print 1= 1
Screenshot
