What is difference between ++x and x++ in Java ? Are those same or not ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ?

3rd Nov 2022, 3:17 AM
Sai Uttej R
Sai Uttej R - avatar
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
3rd Nov 2022, 4:12 AM
Emerson Prado
Emerson Prado - avatar
0
interesting tidbit: there is discussion that pre increment is faster. https://leimao.github.io/blog/CPP-Pre-Increment-VS-Post-Increment/
3rd Nov 2022, 5:27 AM
Bob_Li
Bob_Li - avatar