# difference between x++ and ++x in java

posted Oct 27

++ operator:-

++ operator is called as the incerement operator.it will increment the given data of variable by 1. They can be further divided into two types as pre increment and post increment operator.

Pre incement operator:-

++x is called as pre increment operator on the variable x.let us say we have data 5 inside the variable x.

``````int x=5

int y=(++x)

System.out.print(x+” “+y)``````

Output:-6 6

In the above example firstly the value of x will be incemented by 1 ie to 6.After that it the value of x will get assigned to the variable b.so both the variable will hold 6.

Post increment operator:

x++ is called as post increment operator on the variable x.let us take the same data as in previous example.

``````int x=5

int y=(x++)

System.out.print(x+” “+y)``````

Output:-6 5

Now in this example there is a bit change in the output. let us find out why it happened. So, after initializing 5 to x we are performing post increment  operation on it. In post increment first the value will be assigned to the variable y then it will be incremented in the variable x.

When we are applying this operator to some operand then the

let us suppose there there is an integer variable x.we have initialized the variable with a value let us say 5.

int x=5;

3,280 points
7 4 2