Search code examples
javaincrementpost-incrementpre-increment

post increment behaviour


i have small doubt.why the below code is printing value i=2.

int i=2;
i=i++;
System.out.println(i);

can someone please explain me what is happening in line no 2.

so there is no meaning here of doing ++ here?

Thanks


Solution

  • i=i++;
    

    Because first the assignment happens, then the increment applies.

    Something like:

    first i gets 2, then ++ operation happens, but results won't be re-assigned to i, so i value will remain as 2.