This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# Increment operator in assignment

Selvakumar Arumugam
Greenhorn
Posts: 2
Anybody please explain what is happening here?
--------------
int i =0;
i = i++;
i = i++;
i = i++;
System.out.println(i);
--------------
I was expecting i to be 3. But it's printing 0.

Vicken Karaoghlanian
Ranch Hand
Posts: 522
Hi Selvakumar and welcome to the ranch, the post increment operator always returns its old value then increment it.

Thomas De Vos
stable boy
Ranch Hand
Posts: 425
The reason is that the ++ operator is a postfix operator.
You can read x++ also as x = x = (x+1)
This means that the value of x in your case 0 is first assigned to x and then incremented. The value is incremented after the assignment operation.
The second time around the value of i will be still 0 and then assigned first and incremented etc.

Selvakumar Arumugam
Greenhorn
Posts: 2
Vicken and Thomas Thanks for the reply. I am clear about it.
Thanks!