2

I'm new to java. Just found out that in the expression, y=x++, y assumes the value of x and x becomes +1. Forgive me if I sound stupid, but according to the order of precedence, assignment operators is at the end. So isn't x++ supposed to happen first followed by the assignment. Thanks in advance.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • I have a feeling you are confused between y = x++; and y = ++x; – Stultuske Jun 12 '21 at 07:36
  • 1
    Yes, but `++` and `--` are kind of special, [see here](https://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java). That's why I suggest to use `++` and `--` only as standalone expression. – MC Emperor Jun 12 '21 at 07:40
  • 2
    correct about precedence but [Documentation](https://docs.oracle.com/javase/specs/jls/se16/html/jls-15.html#jls-15.14.2-200): "... The value of the postfix increment expression is the value of the variable *before* the new value is stored. ,,," (The value being assigned is not the variable's one (that would kind of mean higher precedence of assignment) but the result of the post-increment **expression**, by definition, the variable value **before** incrementing) –  Jun 12 '21 at 07:51
  • It's not about priority. The postincrement/postdecrement operators update the operand after its value has been taken, that's all. – user207421 Jun 12 '21 at 07:51

3 Answers3

4

Q: So isn't x++ supposed to happen first followed by the assignment.

A: Yes. And that is what happens. The statement y = x++; is equivalent to the following:

temp = x;      // | This is 'x++'
x = x + 1;     // | (note that 'temp' contains the value of 'x++')

y = temp;      // This is the assignment.

But as you see, the order of the operations (++ and =) doesn't affect what the operations actually do.

And therefore ...

Q: How are y=x++ and y=x-- different when the assignment operators has the least priority?

A: They aren't.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

Operator precedence and evaluation order are two different concepts and they're not related.

If you have a() + b() * c(), that doesn't mean that b() and c() get invoked first because * has a higher precedence than +. a() still gets evaluated first. Evaluation order is typically left-to-right unless stated otherwise.

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

https://docs.oracle.com/javase/specs/jls/se14/html/jls-15.html#jls-15.7

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
0

y=x++ is assigning the value of x to y and then x gets incremented. The x++ operation is called a post-increment.

Here's some code that you can run for illustration:

int x = 0;
System.out.println("Illustration of post-increment");
System.out.println(x);
System.out.println(x++);
System.out.println(x);

int y = 0;
System.out.println("Illustration of pre-increment");
System.out.println(y);
System.out.println(++y);
System.out.println(y);

Beru
  • 657
  • 1
  • 6
  • 21