+ 4

Guys, what is the difference between x+, x++, ++x and the same as x-, x--, --x in java? thank you in advance!

15th Oct 2016, 6:22 PM
Nareg
Nareg - avatar
2 Antworten
+ 9
if you write x+ or x- it will cause an error because there is no meaning in them. However for x++ and x-- 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, 6:37 PM
Lara
Lara - avatar
+ 6
haha thanks Lara:p
15th Oct 2016, 6:39 PM
Nareg
Nareg - avatar