0
Not clear about postfix operators, could someone give me a more detailed explanation?
I don't understand why after doing this: int x = 67; int y = x++; // result is 67 for y and 68 for x. I would like someone to explain this to me with great details. Thanks in advance!
2 odpowiedzi
+ 1
When postfix is used the current value of the variable is used first and then the increment/decrement is done.
int x = 5;
int y = x++; // current value of x (5) is applied to variable y then incremented to 6.
System.out.println(y++); // Here the value of y (5) is printed and then it is incremented to 6.
When prefix is used the increment/decrement happens first then the value is used.
int x = 5;
int y = ++x; // current value of x (5) is incremented to 6 then it is applied to the variable y. y == 6
System.out.println(++y); // Here the value of y (6) is incremented to 7 then 7 is printed.
0
int x = 67;
int y = x++; // this is incrementing the variable x by 1 and assigning it to y.
If you printed out y you would get 67 as it was assigned as 67 then incremented(++)
But if you did a prefix instead of postfix then you could assign y to 68.