DEV Community

Samiur
Samiur

Posted on • Updated on

What is x++ in java

In java, an operand can be either a value of constant or a variable. Arithmatic operations are classified in 3 type, unary, binary and complex operation.

Unary operation: where only a single operand and a single operator perforemd the operation on it.
Binary operation: where two operand and a single operator perforemd the operation in between them.
Complex operation: which performed in between more than two operand.
What is x++
Here x is an operand which is a variable with a numeric value type like short, int, float, double or long etc. the ++ is an unary (icremental) operator which increase the value of the variable x by 1.

By the definition, x++ is a postfix form. The variables value is first used in the expression and then it is incremented after the operation. In simple, it is shorthand for i += 1.

Hence the operator placed at the end of the operand so that the containing statement will be interprated first and then will be performed the unary (increment) operation.

public class KodLogs{

 public static void main(String []args){
    int x = 50;
    //First will be printed 50, then it will be increased by 1.
    System.out.println(x++); 
    System.out.println(x); 
 }
Enter fullscreen mode Exit fullscreen mode

}
Most of the time it used for incrementing local value inside the loop.

public class KodLogs{

 public static void main(String []args){
    for (int x = 0; x < 5; x++) {
         System.out.println(i);
    }

 }
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)