Java

increment and decrement operators in java with example using NetBeans

Description:

The increment and decrement operators are unary operators with one operand each. The increment operator increments the target value by 1, and the decrement operator decrements the target value by 1. Here, I will explain how to use the increment and decrement operators in Java.

In this article you will learn the following topics:

  • What are the increment and decrement operators?
  • difference between prefix and postfix




What are the increment and decrement operators?

The increment operator increments the target value by 1, and the decrement operator decrements the target value by 1. There are two types of operators:

operator Use case meaning
++ A++ or ++A increase the value of A by 1
A– or –A decrease the value of A by 1

Use the increment operator, for example:

int number = 20;

number++;

System.out.println(number); // prints 21

Similarly, the decrement operator can be used, for example:

int number = 20;

number–;

System.out.println(number); // prints 19

Both operators directly increment or decrement the target value by 1. Therefore, you cannot use this operator with integer literals or floating-point literals. Only available for variables.

int number;

number = 5++; // compilation error

Both operators can be placed before as well as after the target value.

int number = 5;

number++;

++number;

number–;

–number;

However, it is necessary to be careful because the result may differ depending on which side is placed (explained later).



How to use Increment and Decrement Operators in java:

Let’s create a simple sample program and try it. After writing the following in a text editor it as incrementAnddecrement.java

And write the following code in the command terminal:

javac incrementAnddecrement.java

and press enter. Then write java incrementAnddecrement and press enter, but in my case, I am using the NetBeans ide.

package com.mycompany.javabasics;

/**

 *

 * @author Fawadkhan

 */

public class incrementAnddecrement {

    public static void main(String[] args){

    int number = 20;

    number++;

    System.out.println(number);

    number–;

    System.out.println(number);

    ++number;

    System.out.println(number);

    –number;

    System.out.println(number);

  }

}

Output:

increment and decrement operators

We used the increment and decrement operators alternately to increment and decrement the value stored in the variable.



difference between prefix and postfix

The increment and decrement operators each have a prefix and a postfix.

int number = 5;

 //Postfix

number++;

number–;

// prefix

++number;

–number;

In the following usage, the result is the same regardless of prefix or postfix.

int x = 20;

x++;

System.out.println(x);  // 21

int x = 20;

++x;

System.out.println(x);  // 21

However, if the result of an operation using the increment operator or decrement operator is assigned to another variable, the result will be different.

int x = 20;

int y;

y = x++;

System.out.println(x);  // 21

System.out.println(y);  // 20

int x = 20;

int y;

y = ++x;

System.out.println(x);  // 21

System.out.println(y);  // 21

With postfix, the value of variable x before it was incremented is assigned to variable y first, then the value of variable x is incremented. On the other hand, if the prefix is ​​used, the value of variable x is incremented by 1 and then the incremented value is assigned to variable y. Therefore, the result is different between prefix and postfix.



So the postfix operation can be rewritten as:

y = x++;

I mean

y = x;

x = x + 1;

Same as

On the other hand, the prefix operation can be rewritten as:

y = ++x;

I mean

x = x + 1;

y = x;

Same as

When using the increment and decrement operators, think carefully about whether using prefix or postfix will do what you think it does.



Example: how to use the Prefix and Postfix operators in java:

package com.mycompany.javabasics;

/**

 *

 * @author Fawadkhan

 */

public class incrementAnddecrement {

    public static void main(String[] args) {

        int Postfix_Number = 20;

        Postfix_Number++;

        System.out.println(“Postfix Value:” + Postfix_Number);

        int Prefix_Number = 20;

        ++Prefix_Number;

        System.out.println(“Prefix Value:” + Prefix_Number);

    }

}

Output:

increment and decrement operators




Examples: increment operator or decrement operator is assigned to another variable:

package com.mycompany.javabasics;

/**

 *

 * @author Fawadkhan

 */

public class incrementAnddecrement {

    public static void main(String[] args) {

        System.out.println(“Value is assigned to another variable”);

        int Postfix_number = 20;

        int y;

        y = Postfix_number++;

        System.out.println(“Incremented Value: ” + Postfix_number); 

        System.out.println(“Actual value: ” + y); 

        System.out.println();

        System.out.println(“Value is assigned to another variable”);

        int Prefix_number = 20;

        int y2;

        y2 = ++Prefix_number;

        System.out.println(“Incremented Value:” + Prefix_number); 

        System.out.println(“Actual value: ” + y2); 

    }

}

Output:

increment and decrement operators

I explained how to use the increment and decrement operators in Java.

Related Articles

Leave a Reply

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

Back to top button