+ 2
Don't understand this Java answer
When I run this x = 10; println(++x + 1); println(x++ + 1); println((x)); The answer is 12 12 12 Why isn't the answer for the second line of code (println(x++ + 1) 13? The previous number is 12, so why isn't it 12 + 1?
4 odpowiedzi
+ 16
1) 11+1 //++10 + 1
2) 11+1 //11++ + 1
3) 12 //12
+ 4
Gaurav your answers are great as always
+ 3
x++ - postfix incrementation
(first the value of x is returned, then x is incremented with 1)
++x - prefix incrementation (first x is incremented, then x is returned)
1. x = 10,
++x + 1 is equal to:
x = x + 1, then x + 1 = 11 + 1 = 12
2. x = 11,
x++ + 1 is equal to:
x + 1 = 11 + 1 = 12, then x = x + 1
3. x = 12
+ 1
Thank you Gaurav and Boris. Boris, the details you provided helped a great deal in understanding the process.