+1

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

8/4/2019 5:31:53 PM

Sara

5 Answers

New Answer

+4

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

+4

Because you are using postfix x++ instead of prefix ++x. Postfix increments after assignment prefix increments before.

+2

Because in y first x equal 34 then y equal 35 for this problem chang y = ++x then print y

+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

+1

because, the variable x hasnt changed , it is declared as x = 34