-3

Given:

int r=1;
r=r++  + r++ + r++;
System.out.println("r:" + r);

Why is this returning 6 instead of 7.

I can get the order of how the above is evaluated e.g. 1 + 2 + 3 = 6 but not sure why the last 'r++' is not reflected in the system.out.

Looking at below:

int i=1;
int r=i++ + i++ +  i++;
System.out.println("r:" + r);
System.out.println("i:" + i);

in this example 'r' is 6 and 'i' is 4, as it should be, and the last 'i++' is reflected in the result printed by System.out.

user207421
  • 305,947
  • 44
  • 307
  • 483
tatamata
  • 41
  • 6
  • 3
    Because `r = r++ + r++ + r++ = 1 + 2 + 3 = 6`. You literally assign `r` the value `6`, thus `r`'s value is `6`. – Turing85 Feb 29 '20 at 22:15
  • The first `r++` sets `r` to `2` (but returns `1`), so it is not the last "r++" but the first "r++" you don't see. – Elliott Frisch Feb 29 '20 at 22:19
  • 2
    Side note: this code should be taken out back and shot – Hovercraft Full Of Eels Feb 29 '20 at 22:22
  • 3
    @HovercraftFullOfEels Shootin' is too good for it, get the honey and a spade. – Elliott Frisch Feb 29 '20 at 22:24
  • yes so : 1 + 2(after first postfix eval) +3 (after second postfix eval) =6 ==> the last postfix (r++) is never evaluated. – tatamata Feb 29 '20 at 22:24
  • 1
    @HovercraftFullOfEels The code? Or the coder? =) – Turing85 Feb 29 '20 at 22:24
  • 1
    @Turing85: I was trying to be generous – Hovercraft Full Of Eels Feb 29 '20 at 22:25
  • 1
    @tatamata this isn't true. It is evaluated. The effect just cannot be observed. – Turing85 Feb 29 '20 at 22:25
  • @Turing85 Can you explain how is the last r++ reflected step by step. I see below being evaluated: – tatamata Feb 29 '20 at 22:42
  • The first r++ sets r to 2 (but returns 1) + The second r++ sets r to 3 (but returns 2 +) The third r++ sets r to 4 (but returns 3) =>6 The last increment is not reflected. – tatamata Feb 29 '20 at 22:42
  • the expression `r++ + r++ + r++` is evaluated left to right. After this, `r` has (for a very short moment) the value `4`. The assignment is evaluated last. Since the whole expression evaluates to `6` (`1 + 2 + 3`), the value `6` gets assigned to `r`. – Turing85 Feb 29 '20 at 22:45
  • @tatamata The last post-increment is executed and then the assignment is executed. 'Not reflected' is meaningless. – user207421 Feb 29 '20 at 22:45
  • @Turing85:"r has (for a very short moment) the value 4" => my point exactly. I can see why the compiler may ignore this but I would expect this to be 7, as when 6 is placed on the stack I would expect the postfix to kick in as with any other postfix statement. What I was looking for is for somebody to explain in a bit more detail why is that not the case. Thanks for taking interest. – tatamata Feb 29 '20 at 22:52
  • @tatamata The compiler doesn't 'ignore this', and there is no evidence here to the contrary. But as the last post-increment is immediately followed by the assignment, and the value being assigned is 6, the side-effect disappears. The value of the expression is never 7. – user207421 Feb 29 '20 at 22:55
  • 1
    No. The postincrement is evaluated before the assignment. After the assignment, the postincrement already happened. – Turing85 Feb 29 '20 at 22:55
  • 1
    And all of this shows why it is, in general, a bad idea to include multiple pre-, post-, and assignments of the same variable in a single statement. While Java allows this, and is specific as to the result, one never knows what the true intent of the author is (did he really know the rules?), or what the next maintainer will think the result should be (does she really know the rules?) – FredK Feb 29 '20 at 23:09
  • This is actually one of the sample java exam questions I got from a friend which I found interesting and ended up questioning the answer. – tatamata Feb 29 '20 at 23:48

1 Answers1

0

Since the expression r++ returns the value of r and THEN it increments it by one. Note that it is possible to write ++r which increments the value before returning it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 3
    OP is well aware of the semantics of the postincrement. This does not answer the question. – Turing85 Feb 29 '20 at 22:30
  • 2
    @Turing85 I'm not sure he is actually. He seems very confused. – user207421 Feb 29 '20 at 23:00
  • @ user207421 - He is aware :) LOL but was questioning the rational – tatamata Feb 29 '20 at 23:49
  • @tatamata I would call it semi-aware. I still don't understand where you got the value 7 from. – user207421 Mar 01 '20 at 02:31
  • @user207421 - aren't we all? try and read it again it may make more sense.The last r++ is increased for a moment but is never actually taken into account. The explanation that kind of makes sense to me is from Turing85: "the postincrement is evaluated before the assignment." The last increment is evaluated but not ever taken into account - that is what I found interesting. I chose to accept a duplicate post, as we were focusing how the code is 'stupid', since that was not the point of the question. – tatamata Mar 01 '20 at 13:32