r/javahelp • Nooblet Brewer • May 29 '20

Solved Can Someone explain why x = x++; doesn’t change the value of x?

This is probably a common ask but it’s been bugging me. I was told that x++ would use its original value in the assignment, then increment to x+1 afterwards.

Let’s say

y = 2 + x++;//x was 3.

y now equals 5 and x equals 4 right?

Why doesn’t x = 2 + x++; work the same? Is it because Java knows that trying to define the same variable as 2 different things on the same line is confusing?

Edit: I understand it now, thanks so much for your help!!

17 Upvotes

18 comments sorted by

12

u/rickkyaa May 29 '20

I think it is because you define x value on line X = 2 + x++; // x is 3.

When x++ is is executed the x value becomes 4, however it’s value is overriden by the sum of 2 + x++ (2 + 3) and in this way x value is 5.

Hope it this helped.

17

u/selrahal May 29 '20

rickkyaa is correct, the critical difference is outlined in the docs[1] in here:

The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value.

So first the runtime will evaluate the expression 2 + x++and then it will assign that value to x. Per the docs, the postfix version evaluates to the original value, that means x++ evaluates to 3. The expression evaluates to 5 and that value overrides x in the assignment. x was temporarily 4 between the evaluation of x++ and the assignment.

[1] https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

6

u/VanVleet-goes-for-22 Nooblet Brewer May 29 '20

x was temporarily 4

I think this is the part I’m getting stuck at conceptually. If I break it down line by line

x = 2 + x++

  • x++ will be fed into the LHS as 3 because it hasn’t incremented it yet

  • 2 + 3 = 5, that value is assigned to x

  • DONE

I guess my question is, at what points is x = 4? Is it once x++ is run until x gets reassigned?

6

u/rickkyaa May 29 '20

Yes right after x++ is executed x becomes 4, but is imediately overriden by the new value which is 5 (2 + x++[3 when adding but 4 in the memory])

5

u/selrahal May 29 '20

Exactly, in fact we can add some logs during this temporary time:

public class IncTest {
  public static void main(String[] args){
    int x = 3;
    System.out.println("first x=" + x);
    x = 2 + logIncrement(x);
    System.out.println("now x=" + x);

  }

  public static int logIncrement(int x){
    System.out.println("Before ++: " + x);
    int toReturn = x++;
    System.out.println("x++ result: " + toReturn);
    System.out.println("X after X++: " + x);
    return toReturn;
  }
}

I highly encourage you to copy past this to a IncTest.java file, compile it with javac IncTest.java and then run it with java IncTest. Nothing quite as helpful as messing with the code, here is the output:

$ java IncTest
first x=3
Before ++: 3
x++ result: 3
X after X++: 4
now x=5

2

u/VanVleet-goes-for-22 Nooblet Brewer May 29 '20

Ok this + the comments above make sense. I guess what was confusing me was that I didn’t really think of the parallelism between the “x” being used in the calculation and the “x” being incremented.

So even though x++ did increment x from 3 to 4, the value we used to calculate the RHS was still 3 and therefore our value of x (which was at one point 4) gets overwritten to 2+3 = 5.

Thanks!

3

u/morhp Professional Developer May 29 '20 edited May 29 '20

Exactly, only its not really parallel, the ordering of such operations is clearly defined in Java.

First java calculates the RHS during which x++ is executed, then it assigns that result to x again. It's completely logical if you treat it this way.

1

u/m1ss1ontomars2k4 May 30 '20

Well that's because it's not parallel at all...but yeah, that explanation is correct.

14

u/sucmadik May 29 '20

The x++ is an expression which means it returns a value. Now in the case of x++, the incrementation happens but it's the original value of x that is returned. Contrarily to the ++x in which the incrmentation happens but the new value is returned.

4

u/mjg123 May 29 '20 edited May 29 '20

In an assignment, the RHS is evaluated then the LHS variable is assigned that value.

If x=2, the line x = x++; goes as follows:

  • the RHS of x++ evaluates to 2 (and in evaluating it sets the value of x to 3).

  • Then the value of x is set to 2 (replacing the 3 which was made in the previous step)

Edit: RHS/LHS is "right-hand side" and "left-hand side", in case that wasn't clear.

3

u/SSOMGDSJD May 29 '20

Y = 2 + X++ says y is equal to the sum of 2 and x, then increase x by one.if you want x to be increased then added to 2 use

Y = 2 + ++x

3

u/TheCrazyPhoenix416 May 29 '20

x++ is an operator. It does the same thing as:

int increment(int& x) {
    int tmp = x;
    x = x + 1;
    return tmp;
}

As you can see, it increments it, but returns the value before incrementing.
Then if you set x back to x++, you're incrementing x, then setting x back to it's prior value.

It would be the same as the following code:

// x++
int tmp = x;
x = x + 1;

// x = x++
x = tmp

1

u/cremmie May 29 '20

It will be changed on next line

1

u/arthurdent May 30 '20

Are you sure? You should try it.

-4

u/Mordan May 29 '20 edited May 29 '20

the question is moot.

This kind of code is banned in my code base.

Anyone using this crap without explaining things in a comment is fired.

Java is about readability. One line. One statement.

x++ was completely removed in Swift.. For good reasons.

3

u/VanVleet-goes-for-22 Nooblet Brewer May 29 '20

I agree that this will never be used outside of theory but I’m studying for the OCA and this was one of the questions.

0

u/[deleted] May 30 '20

[deleted]

2

u/VanVleet-goes-for-22 Nooblet Brewer May 30 '20

I agree that “my way” of doing is stupid, but it wasn’t my choice lol. I’m studying for the OCA exam and this was a question. The book told me x = x++; would give me x but for me, just taking someone’s word for it won’t help me in the long run, so I wanted to get a better fundamental understanding behind why