increment and decrement operators in Java (In Depth) - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to see one of the most used and confused operators in java. That is increment and decrement operators.
Increment and decrement operators are unary operators. We can only apply these operators on a single operand, hence these operators are called as unary operators.
we can apply these unary operators on all primitive types except Boolean.
the increment operator is an operator which is used to increase the value of a variable by 1, on which it is applied.
Again these increment operators are two types:
Pre increment (++x)
Post increment (x++)
If an Increment operator is used in front of an operand, then it is called  as Pre Increment operator.
Syntax:
Example:
class PreIncrement {
       
    public static void main(String[] args) {       
        int x = 10;       
        int y = ++x;       
        System.out.println("y value is: " + y);   
    }

}
Output:
y value is: 11
If an Increment operator is used after an operand, then is called Post Increment operator.
Syntax:
Example:
class PostIncrement {

    public static void main(String[] args) {       
        int x = 10;       
        int y = x++;       
        System.out.println("y value is: " + y);   
    }

}
Output:
y value is: 10
Post increment operator is applied  on ‘x’, here the case is exact opposite of pre increment, first the value of variable ‘x’ is assigned to the variable ‘y’ and then the value of ‘x’ is incremented  by 1 .
As per example, the initial value of ‘x’ is 10. After applying post-increment operator the current values of ‘x’ (i.e, 10) is assigned to y, and then the value of ‘x’ is incremented by 1. So when displaying variable ‘y’ it is showing as 10.
The Decrement operator is an operator which is used to decrease the value of the variable by 1, on which it is applied.
Like increment operators, decrement operators are also 2 types,
Pre decrement (- -x)
Post decrement (x- -)
If a decrement operator is used in front of an operand, then it is called Pre decrement operator.
Syntax:
Example:
class PreDecrement {
    public static void main(String[] args) {
        int x = 10;
        int y = --x;
        System.out.println("y value is: " + y);
    }
}
Output:
y value is: 9
Pre decrement operator is applied on ‘x’, first, the value of ‘x’ will be decremented by 1 and then the decremented value will be assigned to the variable ‘y’.
As per example, the initial value of ‘x’ is 10. After applying pre decrement operator on ‘x’, the value of ‘x’ is decremented by 1 (i.e., 9) and that value is assigned to the variable ‘y’. So, when we display the variable ‘y’ it is showing as 9.
If a decrement operator is used after an operand, then it is called Post decrement operator.
Example:
class PostDecrement {
    public static void main(String[] args) {
        int x = 10;
        int y = x--;
        System.out.println("y value is: " + y);
    }
}
Output:
y value is: 10
Post decrement operator is applied on ‘x’, here the case is the complete opposite of pre decrement operator, first, the value of variable ‘x’ is assigned to the variable ‘y’ and then the value of ‘x’ is decremented by 1.
Syntax:
As per example, the initial value of ‘x’ is 10. After applying post decrement operator on variable ‘x’ the current values of ‘x’ (i.e, 10) is assigned to ‘y’, and then the value of ‘x’ is decremented by 1. So when displaying the value of ‘y’ it is showing as 10.
We can apply Increment and decrement operators only for variables but not for constant values. If we apply, then we will get compile time error.
class IncAndDecOperatorsDemo {

    public static void main(String[] args) {       
        int x = 10;       
        int y = ++10;       
        System.out.println("Value of y : " + y);

           
    }

}
We can’t apply the nesting on increment and decrement operators.
class IncAndDecOperatorsDemo {

    public static void main(String[] args) {       
        int x = 100;       
        int y = --(--x);       
        System.out.println("Value of y : " + y);

    }

}
We can’t apply increment and decrement operators on final variables.
class IncAndDecOperatorsDemo {

    public static void main(String[] args) {       
        final int x = 100;       
        int y = ++x;       
        System.out.println("Value of y : " + y);

    }

}
We can’t apply increment and decrement operators on boolean types.
class IncAndDecOperatorsDemo {

    public static void main(String[] args) {
        boolean b = true;
        --b;
        System.out.println(b);

    }

}
class IncrementOperator {

    public static void main(String[] args) {       
        int x = 10;       
        x = x++;       
        x = x++;       
        x = x++;       
        x = x++;       
        x = x++;       
        System.out.println("The Value of x is : " + x);

    }

}
Output:
Value of x : 10
The Step by Step Analysis on Output:
STEP 1 : Initial value of ‘x’ = 10;
STEP 2 : The value of ‘x’ is post incremented and assigned again to ‘x’.
The variable ‘x’ will be incremented first but the previous ‘x’ value (10) is assigned again to ‘x’ variable, and the incremented (11) value will be used after assigning. But in this example, the next value of ‘x’  is overridden by previous value (10) always.
STEP 3: The value of ‘x’ is post incremented and assigned to ‘x’ only.
STEP 4: The value of ‘x’ is post incremented and assigned to ‘x’ only.
STEP 5: The value of ‘x’ is post incremented and assigned to ‘x’ only.
class IncrementOperator {

    public static void main(String[] args) {       
        int x = 1;       
        x = x++ + ++x + x++ + ++x + ++x;       
        System.out.println("Value of x : " + x);

    }

}
Output:
Value of x : 18
The Step by Step Analysis on Output:
STEP 1 : initial ‘x’ value is 1
STEP 2 : x++ (1)
STEP 3 : ++x (2+1=3)
STEP 4 : x++ (3+1 (Post increment) =3)
STEP 5 : ++x (4+1 = 5)
STEP 6 : ++x (5+1=6)
STEP 7 : Add values from STEP 2 to STEP 6 (1+3+3+5+6)
STEP 8 : Answer is18
Happy Learning 🙂

Difference between b++ and b=b+1
Technical Features of Java Programming
Python List Data Structure In Depth
Python Tuple Data Structure in Depth
Hello World Java Program Example
Java instanceof Operator
== Versus .equals() in Java
Python Set Data Structure in Depth
Lambda Expressions in Java
Java variable types Example
PHP Operators Example Tutorials
Python Operators Example
C Program Addition and Subtraction without using + – Operators
How HashMap Works In Java
What is Java Arrays and how it works ?

Difference between b++ and b=b+1
Technical Features of Java Programming
Python List Data Structure In Depth
Python Tuple Data Structure in Depth
Hello World Java Program Example
Java instanceof Operator
== Versus .equals() in Java
Python Set Data Structure in Depth
Lambda Expressions in Java
Java variable types Example
PHP Operators Example Tutorials
Python Operators Example
C Program Addition and Subtraction without using + – Operators
How HashMap Works In Java
What is Java Arrays and how it works ?
 


Δ
 Install Java on Mac OS
 Install AWS CLI on Windows
 Install Minikube on Windows
 Install Docker Toolbox on Windows
 Install SOAPUI on Windows
 Install Gradle on Windows
 Install RabbitMQ on Windows
 Install PuTTY on windows
 Install Mysql on Windows
 Install Hibernate Tools in Eclipse
 Install Elasticsearch on Windows
 Install Maven on Windows
 Install Maven on Ubuntu
 Install Maven on Windows Command
 Add OJDBC jar to Maven Repository
 Install Ant on Windows
 Install RabbitMQ on Windows
 Install Apache Kafka on Ubuntu
 Install Apache Kafka on Windows
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String