+ 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?

7th Feb 2018, 2:53 PM
Christine Bell
4 odpowiedzi
+ 16
1) 11+1 //++10 + 1 2) 11+1 //11++ + 1 3) 12 //12
7th Feb 2018, 3:47 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 4
Gaurav your answers are great as always
7th Feb 2018, 4:04 PM
Sharofiddin
Sharofiddin - avatar
+ 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
7th Feb 2018, 3:06 PM
Boris Batinkov
Boris Batinkov - avatar
+ 1
Thank you Gaurav and Boris. Boris, the details you provided helped a great deal in understanding the process.
7th Feb 2018, 9:30 PM
Christine Bell