Pre and Post Decrement Operator in Java
Here are the implementation of Pre and Post Decrement Operator in java language:
public class Main {
public static void main(String[] args) {
int num1 = 15, num2 = 20;
int postNum = num1--; // Post Decrement
int preNum = --num2; // Pre Decrement
// Printing value of postNum after post decrement.
System.out.println("postNum = " + postNum);
System.out.println("num1 = " + num1);
// Printing value of preNum after pre decrement.
System.out.println("preNum = " + preNum);
System.out.println("num2 = " + num2);
}
}
Output
postNum = 15 num1 = 14 preNum = 19 num2 = 19
Pre and Post Decrement Operator in Programming
Pre-decrement and post-decrement are the two ways of using the decrement operator to decrement the value of a variable by 1. They can be used with numeric data type values such as int, float, double, etc. Pre-decrement and Post-decrement perform similar tasks with minor distinctions.
Table of Content
- What is a Pre-Decrement Operator?
- What is a Post-Decrement Operator?
- Pre and Post Decrement Operator in C
- Pre and Post Decrement Operator in C++
- Pre and Post Decrement Operator in Java
- Pre and Post Decrement Operator in Python
- Pre and Post Decrement Operator in C#
- Pre and Post Decrement Operator in Javascript
- Application of Pre and Post-Decrement Operators