Saturday , August 19 2017
Home / java / increment and decrement operators in Java

increment and decrement operators in Java

In this tutorial, we are going to learn about one of the mostly used and most confused operators in java. That is increment and decrement operators.

increment and decrement operators :

Increment and decrement operators are unary operators. We can only apply these operators on single operand, hence these operators are called as unary operators.

we can apply these unary operators on all primitive types except boolean.

There are two types of unary operators :

  • increment and
  • decrement

Increment operator (++):

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++)

Pre Increment Operator:

If an Increment operator is used in front of an operand, then it is called  as Pre Increment operator.

Syntax:

++x : which increments the value by 1 of ‘x’ variable.

Ex:


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

On the above example, pre increment operator is applied  on x operand , first the value of x will be incremented by 1 and then the incremented value will be assigned to the variable y .

As per example, the initial value of ‘x’ is 10. After applying pre increment operator on ‘x’ the value of ‘x’ is incremented by 1 (i.e., 11) and that value is assigned to the variable ‘y’. So that the final value of  ‘y’ is 11.

Post Increment Operator:

If an Increment operator is used after an operand, then is called Post Increment operator.

Syntax:

x++ : which increase the value by 1 of variable ‘x’.

Ex:


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.

Decrement Operator (- -):

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- -)

Pre Decrement Operator:

If an decrement operator is used in front of an operand, then it is called Pre decrement operator.

Syntax:

– -x : which decrease the value by 1 of variable ‘x’ .

Ex:


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’. Sothat, whe nwe display the variable ‘y’ it is showing as 9.

Post Decrement Operator:

If an decrement operator is used after an operand, then it is called Post decrement operator.

Syntax:

x- – : which decrease the value by 1 of variable ‘x’ .

Ex:

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 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 .

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 value of ‘y’ it is showing as 10.

Limitations of Increment and Decrement Operators:

  • 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);

    }

}

Increment and decrement operators

  • 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);

    }

}

Error

  • 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);

    }

}

Error2

  • 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);

    }

}

Error3Examples on Increment and Decrement Operators:

Example 1:


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 overrided 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.

Example 2:


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 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Binary Literals Java 7 Example Tutorials

Binary literals is one of the new feature added in Java 7. We already know …

Leave a Reply

Your email address will not be published. Required fields are marked *