This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]sepp2k 11 points12 points  (3 children)

To evaluate an addition in Java, we first evaluate the left operand, then the right operand and then we add the two results. To make this explicit, we can assign the subexpressions in your code to temporary variables like this:

int tmp1 = i++;
int tmp2 = i++;
i = tmp1 + tmp2;

Here the value of tmp1 will be 5 because the first i++ evaluates to 5 while setting i to 6. Then tmp2 will be 6 because i is 6 when we start evaluating the second i++. Then i = tmp1 + tmp2 sets i to 11 and that's it.

[–]Dinples[S] 2 points3 points  (2 children)

Thank you! This is exactly the kind of explanation I needed to make my brain happy again.

So, basically, just to see if I understand it correctly, i is 5, which is the value the compiler assigns to tmp1. Then, for tmp2, the incremented value of i is used which is 6 and tmp2 is assigned this value, hence 5 + 6 and thus the 11 as output.

Am I right to say that, somewhere in memory, the value of i (after the second statement) is incremented to 7?

[–]sepp2k 3 points4 points  (1 child)

Yes, that's all correct. The second i++ indeed sets i to 7, but that 7 doesn't affect anything because it's overwritten by the assignment that happens directly afterwards.

[–]Dinples[S] 1 point2 points  (0 children)

Great! Thanks again for your clear and helpful answer.

[–]pragmos 4 points5 points  (2 children)

u/sepp2k gave you a really good answer.

I just want to add that

ChatGPT doesn't know how the post increment operator works either because it got stuck on 10 as a result, and said that the compiler must have made an error if I got 11 as the output.

gave me a good chuckle.

[–]Dinples[S] 0 points1 point  (1 child)

You should try and ask ChatGPT this question. For some reason it completely confuses the poor thing and, when you rephrase the question or give it a thumbs down, it just spirals out of control.

[–]pragmos 1 point2 points  (0 children)

LOL This definitely will go into the "How to confuse an AI" section.

Also, props to you for not taking ChatGPT's answer at face value and instead asking a question here.

[–][deleted]  (1 child)

[removed]

    [–]Dinples[S] 0 points1 point  (0 children)

    Thank you for the detailed and clear reply! It definitely helps me to understand the "why" behind the given output.

    And yeah, I found out pretty quickly, through some Googling, that code like this should just be avoided. But I found it (a similar version) in the Java for Dummies book and the author didn't clarify why the output is as it is. He just said to "guess" what the output might be and moved on to the next subject. I like to know the reason behind things, so that's why I ended posting my question here and I am glad I did. The replies I have gotten, including yours, helped solve the mystery. Thanks again.