What is the value of x if "x = x++"?

Asked 1 years ago, Updated 1 years ago, 65 views

int x = 7;
x = x++;

What happens when there's a code like this? Does the variable increase after calculation and assign a value to itself? When I compiled it, x still has a value of 7. In my book, it says that the value of x increases... What is it?

java post-increment operator

2022-09-22 13:37

1 Answers

The x itself is incremented. However, x assigns itself the previous x value.

x=x++; In the syntax, x++ returns the value of x before x increases, but returns before x increases. So at x=, the value of x before x is increased, 7 is substituted.

In conclusion, x is increased, but x is substituted with the initial x value, so the value is 7.


2022-09-22 13:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.