What is the difference between a++ and ++a? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

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

15th Oct 2016, 12:46 PM
Lara
Lara - avatar
2 Answers
+ 5
There is no difference when used alone, the difference is when you use them in an expression. a++ evaluates a, then increments it (post-incrementation). ++a increments a, then evaluates it (pre-incrementation). Example: int a = 1; int b = a++; //b = 1, a = 2 System.out.println(b); //prints 1 a = 1; b = ++a; //b = 2, a = 2 System.out.println(b); //prints 2
15th Oct 2016, 1:10 PM
Zen
Zen - avatar
+ 1
when your program runs, the a++ increment after all instructions, when you use ++a first your variable increment the value then the program flows down and finish with the new value
22nd Oct 2016, 4:37 PM
Neo
Neo - avatar