+ 1

--x and x--

why do we use --x instead of x-- in this code? Thanks!

2nd May 2017, 8:14 AM
m y
m y - avatar
3 ответов
+ 27
--x is a predecrement. x-- is a postdecrement. int x = 5; System.out.println(--x); // outputs 4 System.out.println(x--); // outputs 4 if only x-- output: 5
2nd May 2017, 8:19 AM
Dev
Dev - avatar
+ 3
Another difference is that x-- makes a copy of x, so it is slower than --x. So if it makes no difference which one you use you should use --x.
2nd May 2017, 9:20 AM
Bebida Roja
Bebida Roja - avatar
0
coz I found that there' no output when I change x-- from --x in the given code. so I was doubt
2nd May 2017, 9:37 AM
m y
m y - avatar