+ 2

X vs y operator

Why is, int x = 34; int y = x++; System.out.println(y); Output : 34 And int x = 34; int y = x++; System.out.println(x); Output : 35

4th Aug 2019, 5:31 PM
Sara
Sara - avatar
5 Réponses
+ 5
Because you safe the valu of x in y and only after that step x gets incrementet by 1 If you would write y = ++x the value will be incrementet first and then it will be saved in y
4th Aug 2019, 5:34 PM
Dragonxiv
Dragonxiv - avatar
+ 4
Because you are using postfix x++ instead of prefix ++x. Postfix increments after assignment prefix increments before.
4th Aug 2019, 5:35 PM
StealthyTaco
StealthyTaco - avatar
+ 2
Because in y first x equal 34 then y equal 35 for this problem chang y = ++x then print y
4th Aug 2019, 5:36 PM
elias sharafi
elias sharafi - avatar
+ 2
Because the first one is postfix it gets its incremented value only in next step of the program.And at the second time it saves the value of previous x
5th Aug 2019, 3:55 PM
Devi Kamaraj..😉
Devi Kamaraj..😉 - avatar
+ 1
because, the variable x hasnt changed , it is declared as x = 34
4th Aug 2019, 5:33 PM
Sara
Sara - avatar