+ 4
Whats the diference between x++ and ++x
9 Answers
+ 13
Even if it's answered a few times i will explain it to you:
The difference lies in the execution order.
When you type x++ it means that the variable is being incremented AFTER it is used.
int x=0;
System.out.println(x++);//the output will be still 0, because it is used first, then incremented
Now again
System.out.println(x); // this will be now 1
_________________
int x=0;
System.out.println(++x);//the output will now be 1 because it is incremented first, then used
Now again
System.out.println(x); // this will be now 1
+ 5
x++ returns value of x and then increments x
y=x++ means y=x and x=x+1
++x increments x and then returns value of x
y=++x means x=x+1 and y=x
+ 4
++a is prefix which increases the variables value and the uses the new value in expression.
a++ is posfix which takes the variable value first in the expression and then increase the variable.
E.g. prefix:
int x = 4;
int y = ++x; // y = 5
prosfix:
int x = 4;
int y = x++;// y = 4
+ 2
is that means ++ is the incrementing
+ 2
++x means pre increment x++ means post increment
example
x=2;
++x;
//x=3
x++;
//x=3
+ 1
Exactly the same thing you can do with - -
+ 1
++x means pre increment i. e. you used the value of incremented x before store it and x++ is post increment u used incremented value after store it.
if x=5;
ex- x=x++ + ++x;
5 + 6 =11;
0
The former is postfix and the latter is prefix .In the former,first the value of x is displayed and then it is increased by one while the case is opposite in the latter.
0
X++ will increment the value after a usage and ++X will first increment the value and then use it , for ex , if x=5 and say
y=++x;
Print y
Print x
Then output will be : 6 6
But if y=x++ then
Print y
Print x
Will give output as:
5 6
So, in ++x it uses modified value instantly and in x++ it uses old value of x and then increment it for further use.