+ 2

What is the difference between x++ and ++x?

1st Feb 2017, 11:24 AM
yatharth jain
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.
1st Feb 2017, 12:12 PM
seamiki
seamiki - avatar
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
5th Feb 2017, 10:33 AM
Anand Bachhav
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.
6th Feb 2017, 5:36 PM
Chinmai Sai
Chinmai Sai - avatar