for the question:
int i=1;
i = i++;
System.out.println(i);
The program prints 1, but I do not get it. Should it be first i get the 1 and then i incremented to 2, so result is 2 for i?
[HD Pg 0, Sec. 6.1.3 - post-and-pre-unary-increment-decrement-operators]
Moderator: admin
-
- Posts: 4
- Joined: Tue Nov 19, 2019 7:01 pm
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 0, Sec. 6.1.3 - post-and-pre-unary-increment-decrement-operators]
But in the last step the value of the expression i++ (which was 1, as recorded before ++ is applied to i) is assigned to i. So, i becomes 2 momentarily (due to i++) but then is set to 1 finally.
The paragraph just above this question actually explains it:
The paragraph just above this question actually explains it:
You need to appreciate the fact that the value of the variable i and the value of the expression i++ (or ++i) are two different things and they may or may not be the same depending on whether you use pre or post form of the increment/decrement operator. When you do int x = ++i; you are not assigning the value of the variable i to x. You are assigning the value of the expression ++i to x independently from the process of applying ++ to i.
Who is online
Users browsing this forum: No registered users and 4 guests