New course! Every coder should learn Generative AI!
Try a free lesson+ 5
What is the difference between a++ and ++a?
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
+ 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