Java-Increment and Decrement Operators

Increment and Decrement operators

Java has two very useful operators. These are the increment and decrement operators :

The operators ++ adds 1 to the operand while - - subtracts 1. For example, the code

changes ‘totel’ to 6. Because these operators change the value of ‘totel‘ variable, they cannot be applied to numbers themselves. For example, 5++ is not a legal statement.

There are actually two forms of these operators; you have seen the “postfix” form of the operator that is placed after the operand. There is also a “prefix” form, ‘++totel’. Both change the value of the variable by 1. The difference between the two only appears when they are used inside expressions. The prefix form does the addition first; the postfix form evaluates to the old values of the variable.



Program Source

class Javaapp {

    public static void main(String[] args) {
        int a = 5;
        int b = 6;
        int x = 10;
        int c = 10*x++;
        int y = 10;
        int d = 10*++y;
        System.out.println("a = "+a);
        System.out.println("b = "+b);
        System.out.println("c = "+c);
        System.out.println("d = "+d);

Leave a Comment