Operator in Java Program – 5

Increment and Decrements Operator in Java Program

Operator is very import for all languages. Operator can calculate, check, assign, increase and decrease the value of variable. So operator is heart of java program. I give simple example of operator in java program.

X++;

In this example X is variable;

++ (increment) operators.

So we can easily understand the import of operator in java program language. In this blog we will discuss on increment [++]  ans decrement [ — ] operators.

Increment operator

++ is symbol of Increment operator.

Using of this operator we can increase one value into variable.

Let’s see simple example.

int x = 10;

x++;    // Here x = x+1        | You can also write ++x; Both same

Now x = 11

Decrement operator

— is symbol of Increment operator.

Using of this operator we can decrease one value from variable.

Let’s see simple example.

int x = 10;

x–;      // Here x = x-1         | You can also write –x; Both same

Now x = 9

 

Program on Increment and Decrement Operator:

class op5

{

          public static void main(String args[])

          {

                   int x = 20;

                   x++;   // x = x+1

                   //x–;   // x = x-1                                  // X = 19

                   System.out.println(“X = ” + x);        // X = 21

                   ++x;   // x = x+1

                   //–x;   // x = x-1                                  // X = 18

                   System.out.println(“X = ” + x);        // X = 22

          }

}

Output:

X = 21

X = 22

About Program [Postfix and Prefix are same]

In this program we can see [x++] and [++x] are both same. We can also say [x++] to postfix and [++x] to prefix. Generally both have some different but in this example we see both types working same to increase value of variable.

x++;    // Increase X = 20 to X=21

++x;  // Increase X = 21 to X=22

 

Program on Increment and Decrement Operator:

class op6

{

          public static void main(String args[])

          {

                   int x = 10;

                   int y;

                   y = x++;      // Step 1: y = x, Step 2 : x++;

                   //y = x–;       // Step 1: y = x, Step 2 : x—; // Y = 10 , X = 9

                   System.out.println(“X = ” + x + “ Y = ” + y); // X = 11 , Y = 10

                   y = ++x;      // Step 1: ++x, Step 2 : y=x;

                   //y = –x;       // Step 1: –x, Step 2 : y=x; // Y = 8 , X = 8

                   System.out.println(“X = ” + x + “ Y = ” + y); // X = 12 , Y = 12

          }

}

Output:

X = 11 Y = 10

X = 12 Y = 12

About Program [Postfix and Prefix are Different]

In this program we can see [x++] and [++x] are both different. We can also say [x++] to postfix and [++x] to prefix. In this example we see both types working different to increase value of Y variable.

y = x++;         // X = 11 Y = 10 (Here Y is not increase)

y = ++x;         // X = 12 Y = 12 (Here Y is increase)

 

Video available on YouTube.

 


[sbscrbr_form]

If you have problem on this blog, you can directly comment or subscribe on website.


Earn money online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

234total visits,2visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

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

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint