Increment and Decrement Operators Tutorial

The Java language has several Unary operators, this tutorial will discuss the proper use of the increment and decrement operators. The increment operator is simply a pair of plus symbols ++ that are placed either before or after an operand (variable++ or ++variable). The decrement operator is simply a pair of minus symbols -- that are placed either before or after an operand (variable-- or --variable). They work by simply incrementing or decrementing the operand by one. These two operators are used quite often in many looping statements.
The prefix (++variable) version evaluates to the incremented value, and the postfix (variable++) version evaluates to the original value; I'll explain what this means later in the tutorial.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md Increment
C:\Java>cd Increment
C:\Java\Increment>Notepad Increment.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class Increment {
    public static void main(String args[]) {
        int a = 1;
        int b = 10;

        a = a + 1; // a equals 2 now
        b = b - 1; // b equals 9 now
        System.out.println("a = " + a + ", b = "+ b);

        a++; // a equals 3 now
        b--; // b equals 8 now
        System.out.println("a = " + a + ", b = "+ b);

        ++a; // a equals 4 now
        --b; // b equals 7 now
        System.out.println("a = " + a + ", b = "+ b);

    }
}

Now switch back to the command prompt (CMD) and type in javac Increment.java and press Enter.
Now type in java Increment and press Enter.


C:\Java\Increment>javac Increment.java
C:\Java\Increment>java Increment
a = 2, b = 9
a = 3, b = 8
a = 4, b = 7


Copy and Paste, or type the following code into the same Notepad file and be sure to save the file when you are done.


class Increment {
    public static void main(String args[]) {

        System.out.println("Prefix and Postfix differences");	
        int x = 1;
        System.out.print("x= "); 
        System.out.println(++x); // Prefix - will this print out 2?
        int y = 1;
        System.out.print("y= "); 
        System.out.println(y++); // Postfix - will this print out 2?
        System.out.println("Why did x and y print different values?");
        System.out.println("The prefix ++x version changed the value of x before ");
        System.out.println("the complete execution of the entire statement.");
        System.out.println("The postfix y++ version changed the value of y after");
        System.out.println("the complete execution of the end of the statement.");
        System.out.println("To prove this, we will simply print the current value of y: "+y);

    }
}

Now switch back to the command prompt (CMD) and type in javac Increment.java and press Enter.
Now type in java Increment and press Enter.


C:\Java\Increment>javac Increment.java
C:\Java\Increment>java Increment
x= 2
y= 1
Why did x and y print different values?
The prefix ++x version changed the value of x before 
the complete execution of the entire statement.
The postfix y++ version changed the value of y after
the complete execution of the end of the statement.
To prove this, we will simply print the current value of y: 2


Copy and Paste, or type the following code into the same Notepad file and be sure to save the file when you are done.


class Increment {
    public static void main(String args[]) {
        int d = 5;
        
        System.out.print("Is d++ == ++d? ");
        System.out.println(d++ == ++d); // false, why?
        /*
            When d++ is evaluated on the left side of the relational operator ==, 
            the value of d is still 5 because the statement has not finished executing.            
            The postfix++ operator will not change the value of d until the statement has finished execution.
            Remember - all Java statements (except blocks) are terminated by a semicolon.
            When ++d is evaluated on the right side of the relational operator ==,
            the value of d is immediately changed to 6 because the ++prefix operator changes the value prior to statement completion.
            The expression d++ == ++d is literally 5 == 6, which of course is false.
        */
        System.out.println("What is the value of d now that the statement has executed? d="+d); // 7, think about it.
        System.out.println();
        System.out.println("--------------------");
        System.out.println("One last thing on Prefix and Postfix differences");	
        
        d = 5; //change d back to 5
        
        System.out.print("Is ++d == d++? ");
        System.out.println(++d == d++); // true, why?
        /*
            When ++d is evaluated on the left side of the relational operator ==,
            the value of d is immediately changed to 6 because the ++prefix operator changes the value prior to statement completion.
            When d++ is evaluated on the right side of the relational operator ==, 
            the value of d is still 6 because the statement has not finished executing.            
            The postfix++ operator will not change the value of d until the statement has finished execution.
            Remember - all Java statements (except blocks) are terminated by a semicolon.
            The expression ++d == d++ is literally 6 == 6, which of course is true.
        */
       System.out.println("What is the value of d now that the statement has executed? d="+d); // 7, think about it.
    }
}

Now switch back to the command prompt (CMD) and type in javac Increment.java and press Enter.
Now type in java Increment and press Enter.


C:\Java\Increment>javac Increment.java
C:\Java\Increment>java Increment
Is d++ == ++d? false
What is the value of d now that the statement has executed? d=7

--------------------
One last thing on Prefix and Postfix differences
Is ++d == d++? true
What is the value of d now that the statement has executed? d=7


Final thoughts

The ++ and -- operators are used quite often in Java programming, especially in looping statements. If you don't thoroughly understand the differences between the postfix and prefix versions of the increment and decrement operators, then please take the time to review this tutorial until you do.


Tutorials