Hello Daniweb,
I can't wrap my head around this minor issue. Maybe it's a known bug and I just can't find the right keywords to google it or maybe it's supposed to be like this. Let me know what you guys think.

public class TestPostfix {
    
    public static void main(String args[]) {
        
        int x = 3;
        
        x = 6 + x++ * 3 / 2 - 3;
        
        System.out.println(x);
        
    }
    
}

Why does that print 7? "x" evaluates to 7 and rightly so, but the increment operator doesn't seem to kick in to set it to 8 before it's printed.

// This prints 9
x = 6 + ++x * 3 / 2 - 3;

The prefix prints 9 like it should, but can someone explain why the post fix isn't evaluating at the end of the statement?

Edited 4 Years Ago by bloodbender: n/a

4 + 6 - 3 = 7.

the first thing you do, is
3 * 3 (x++ means x will only be augmented after the statement has been completed, at which point you overwrite x with the value your equation gives you.)
so, the next thing that will be calculated:
9/2 => 4.5, but since we're working with an int here: 4
then it becomes easy:
x = 6 + 4 - 3 => 10 - 3 => 7

when you say ++x, the value of x will be augmented before it's being used, so:
step 1. 4 * 3 = 12
step 2. 12 / 2 = 6
step 3. 6 + 6 = 12
step 4. 12 - 3 = 9

lol, thanks for your reply stultuske, but I know the order of precedence for calculations. That's not what I was asking. If you read my post again, I said

"Why does that print 7? "x" evaluates to 7 and rightly so, but the increment operator doesn't seem to kick in to set it to 8 before it's printed."

So yes, I know x is supposed to evaluate to 7, exactly how you described it, but after that statement ends and BEFORE it's printed, logically it SHOULD change to 8 because of the x++. That's what I'm asking. Why isn't it doing that? Why does it print just 7?

nope, the original 3 will be augmented, not the result of the equation.
so, after actually using that x, (3*3) it will augment to: 4, but, it won't be used again.
your x will simply be overwritten by the result you get, which is also in my previous post :)

Comments
Well explained point. Thanks bud!

lol, thanks for your reply stultuske, but I know the order of precedence for calculations. That's not what I was asking. If you read my post again, I said


So yes, I know x is supposed to evaluate to 7, exactly how you described it, but after that statement ends and BEFORE it's printed, logically it SHOULD change to 8 because of the x++. That's what I'm asking. Why isn't it doing that? Why does it print just 7?

The answer will be 7 because you are already declaring x=...x++; so the x operator increased but the calculations answer was already stored:

int x = 3;
 int y=3;
        x = 6 + y++ * 3 / 2 - 3;
        System.out.println(y);//y is 4... it did increment
        System.out.println(x);//answer is 7 because the answer to x was already evaluated..

[edit]slow there lol :D

Edited 4 Years Ago by DavidKroukamp: n/a

nope, the original 3 will be augmented, not the result of the equation.
so, after actually using that x, (3*3) it will augment to: 4, but, it won't be used again.
your x will simply be overwritten by the result you get, which is also in my previous post :)

Ahhh, I see what you're saying now. I was under the impression the x won't augment until AFTER the semicolon, which is how I've seen it behave in the past. Now it makes sense. It will augment right after 3 * 3 is evaluated, but then gets overwritten by the overall result of the equation.

Great explanation man! +1 for that.

This question has already been answered. Start a new discussion instead.