Pre and Post Decrement Operator in Java

Here are the implementation of Pre and Post Decrement Operator in java language:

Java
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

Similar Reads

What is a Pre-Decrement Operator?

In pre-decrement operation, the decrement operator is used as the prefix of the variable. The decrement in value happens as soon as the decrement operator is encountered....

What is a Post-Decrement Operator?

In post-decrement operation, the value is decremented after all the other operations are performed i.e. all the other operators are evaluated. The decrement operator is used as the suffix to the variable name....

Pre and Post Decrement Operator in C:

Here are the implementation of Pre and Post Decrement Operator in C language:...

Pre and Post Decrement Operator in C++:

Here are the implementation of Pre and Post Decrement Operator in C++ language:...

Pre and Post Decrement Operator in Java:

Here are the implementation of Pre and Post Decrement Operator in java language:...

Pre and Post Decrement Operator in Python:

Here are the implementation of Pre and Post Decrement Operator in Python language:...

Pre and Post Decrement Operator in C#:

Here are the implementation of Pre and Post Decrement Operator in C# language:...

Pre and Post Decrement Operator in Javascript:

Here are the implementation of Pre and Post Decrement Operator in javascript language:...

Application of Pre and Post-Decrement Operators:

Loop Control: Used to decrement loop control variables in loops.Array Indexing: Employed to access array elements by decrementing the index.Iterator Manipulation: Utilized in data structures like linked lists or iterators to move to the previous element.Control Flow: Modify control flow based on certain conditions.Performance Optimization: Pre-decrement may be more efficient than post-decrement in certain scenarios....

Conclusion:

Pre and post-decrement operators are fundamental tools in programming for decrementing variable values. They are commonly used in loops, array indexing, iterator manipulation, control flow, and performance optimization. Understanding the difference between pre and post-decrement operators and their appropriate usage can lead to efficient and readable code....