+ 2
What is the difference between x++ and ++x?
3 Answers
+ 8
//x increases before operation
int x=0;
int total = ++x + 4
System.out.println(total);//5
System.out.println(x);//1
//x increases after operation
int x=0;
int total = x++ + 4
System.out.println(total);//4
System.out.println(x);//1
Besides this question beingbasked over and over (do a qick search in the Q&A section of the app: probably youll find better answers) the best way to understand things is to try.
the code playground is exactly for this.
By trial/error you'll understand and learn in the fastest way.
0
for example x=8;
x++ means first gives 8 value after add 1 value.
++x means firstly give 1 value then give 8 value.
both increment operator is use in loops
0
x++ means increment the number after the execution of the operation.
int X=2; // value is assigned
int Y=X++;
// Now the value of Y is 2 and value of X will be incremented after the assignment of Y. Now x=3.
If we use Y=++X;
//In this case there is no value assigned for Y. So after the increment of X the value is assigned to Y.



