CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Increment and Decrement Operator in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Increment and Decrement Operator in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 4 min read ยท 0 Comments
Share: in X

Increment and decrement operator in Java are unary operators that are used to increase or decrease the value of a variable by one. The increment operator is ++ and the decrement operator is --.

Table of Contents

  • Uses of increment and decrement operator in Java
  • Pre Increment
  • Post Increment
  • Post Decrement
  • Pre Decrement
  • FAQs
    • What are the increment and decrement operators in Java?
    • What’s the difference between the postfix and prefix forms of the increment and decrement operators?
    • Can the increment and decrement operators be applied to variables of any data type in Java?
    • Are there any side effects or considerations when using the increment and decrement operators?
    • Can the increment and decrement operators be used in loops?

Uses of increment and decrement operator in Java

The increment operator can be used in two ways:

  • Prefix: This is when the operator is placed before the variable, such as ++x. This means that the value of x is incremented first, and then the value is used in the expression.
  • Postfix: This is when the operator is placed after the variable, such as x++. This means that the value of x is used in the expression first, and then the value is incremented.

The decrement operator works in the same way, but it decreases the value of the variable by one instead of increasing it.

Let’s see an example of how auto increment and auto decrement can be used in Java.

Pre Increment

Example:

public class PreIncrementExample {
    public static void main(String[] args) {
        int num = 5;

        // Pre-increment: Increment the value of 'num' by 1 before using it in an expression.
        int result = ++num;

        System.out.println("num: " + num);       // Output: num: 6
        System.out.println("result: " + result); // Output: result: 6
    }
}

In this example, the pre-increment operator ++num increments the value of num by 1 before assigning it to the result variable. As a result, both num and result become 6 after the pre-increment operation.

Post Increment

public class PostIncrementExample {
    public static void main(String[] args) {
        int num = 5;

        // Post-increment: Use the current value of 'num' in the expression and then increment it by 1.
        int result = num++;

        System.out.println("num: " + num);       // Output: num: 6
        System.out.println("result: " + result); // Output: result: 5
    }
}

In this example, the post-increment operator num++ is used in the expression, so the current value of num (which is 5) is used to assign to the result variable. After the assignment, the value of num is incremented by 1. As a result, num becomes 6, but the resultvariable gets the original value of num (5) before the increment.

Post Decrement

public class PostDecrementExample {
    public static void main(String[] args) {
        int num = 10;

        // Post-decrement: Use the current value of 'num' in the expression and then decrement it by 1.
        int result = num--;

        System.out.println("num: " + num);       // Output: num: 9
        System.out.println("result: " + result); // Output: result: 10
    }
}

In this example, the post-decrement operator num-- is used in the expression, so the current value of num (which is 10) is used to assign to the result variable. After the assignment, the value of num is decremented by 1. As a result, num becomes 9, but the result variable gets the original value of num (10) before the decrement.

Pre Decrement

public class PreDecrementExample {
    public static void main(String[] args) {
        int num = 10;

        // Pre-decrement: Decrement the value of 'num' by 1 before using it in an expression.
        int result = --num;

        System.out.println("num: " + num);       // Output: num: 9
        System.out.println("result: " + result); // Output: result: 9
    }
}

In this example, the pre-decrement operator --num decrements the value of num by 1 before assigning it to the result variable. As a result, both num and result become 9 after the pre-decrement operation.

FAQs

What are the increment and decrement operators in Java?

The increment operator (++), represented as “++,” is used to increase the value of a variable by 1. The decrement operator (–), represented as “–,” is used to decrease the value of a variable by 1 in Java.

What’s the difference between the postfix and prefix forms of the increment and decrement operators?

The postfix form (e.g., x++) uses the current value of the variable in an expression and then increments or decrements it. In contrast, the prefix form (e.g., ++x) increments or decrements the variable first and then uses the updated value in the expression.

Can the increment and decrement operators be applied to variables of any data type in Java?

No, the increment (++) and decrement (–) operators are primarily used with integer types, such as int, long, byte, and short. They are not suitable for non-numeric data types like strings or booleans.

Are there any side effects or considerations when using the increment and decrement operators?

Yes, it’s essential to use these operators with caution, especially in complex expressions. For example, if you have expressions like x = x++ or y = --y + y++, the behavior may not be intuitive, and the results can vary. It’s recommended to use these operators in straightforward, understandable ways to avoid confusion.

Can the increment and decrement operators be used in loops?

Yes, these operators are frequently used in loops, such as for and while loops, to control iteration and manipulate loop variables. They can help in incrementing or decrementing loop counters, making loops more efficient and concise.

Related Posts:

  • Default Variable Initialization in Java
  • Control Statements in Java
  • Variable in Java
  • Identifiers in Java
  • What is Java? Exploring Its Key Features, Benefits,…
  • final Keyword in Java
Tags:javalanguage-fundamentals
Was this article helpful?
โ† Previous ArticleArithmetic Operator in Java
Next Article โ†’Comparison or Relational Operators in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap