A student of mine wrote the following code:
public class Counter{
private num = 0;
public void inc(){
num = num++;
}
}
which fails to increment.
Why? If num starts at 0, then the assignment should be num = 0 then num should increment during the postfix incrementation of num!? (of course any experienced programer would have written simply num++)
public class Counter{
private num = 0;
public void inc(){
num = ++num;
}
}
Naturally, works due to the fact that prefix incrementation takes place before assignment.