+ 1
What is difference between ++x and x++ in Java ? Are those same or not ?
Both give the same output so do they have any difference or are they same ?
2 Answers
+ 3
++x increments x, then returns the new value
x++ returns the current value, then increments x
You can only see the difference when using it in an expression like:
x = 1
y = ++x
Now, y equals 2
x = 1
y = x++
Now, y equals 1
0
interesting tidbit: there is discussion that pre increment is faster.
https://leimao.github.io/blog/CPP-Pre-Increment-VS-Post-Increment/