What is x after “x = x++”?

孤街浪徒 提交于 2019-11-26 21:59:48

问题


What happens (behind the curtains) when this is executed?

int x = 7;
x = x++;

That is, when a variable is post incremented and assigned to itself in one statement? I compiled and executed this. x is still 7 even after the entire statement. In my book, it says that x is incremented!


回答1:


x does get incremented. But you are assigning the old value of x back into itself.


x = x++;
  1. x++ increments x and returns its old value.
  2. x = assigns the old value back to itself.

So in the end, x gets assigned back to its initial value.




回答2:


x = x++;

is equivalent to

int tmp = x;
x++;
x = tmp;



回答3:


The statement:

x = x++;

is equivalent to:

tmp = x;   // ... this is capturing the value of "x++"
x = x + 1; // ... this is the effect of the increment operation in "x++" which
           //     happens after the value is captured.
x = tmp;   // ... this is the effect of assignment operation which is
           //     (unfortunately) clobbering the incremented value.

In short, the statement has no effect.

The key points:

  • The value of a Postfix increment/decrement expression is the value of the operand before the increment/decrement takes place. (In the case of a Prefix form, the value is the value of the operand after the operation,)

  • the RHS of an assignment expression is completely evaluated (including any increments, decrements and/or other side-effects) before the value is assigned to the LHS.

Note that unlike C and C++, the order of evaluation of an expression in Java is totally specified and there is no room for platform-specific variation. Compilers are only allowed to reorder the operations if this does not change the result of executing the code from the perspective of the current thread. In this case, a compiler would be permitted to optimize away the entire statement because it can be proved that it is a no-op.


In case it is not already obvious:

  • "x = x++;" is almost certainly a mistake in any program.
  • The OP (for the original question!) probably meant "x++;" rather than "x = x++;".
  • Statements that combine auto inc/decrement and assignment on the same variable are hard to understand, and therefore should be avoided irrespective of their correctness. There is simply no need to write code like that.

Hopefully, code checkers like FindBugs and PMD will flag code like this as suspicious.




回答4:


int x = 7;
x = x++;

It has undefined behaviour in C and for Java see this answer. It depends on compiler what happens.




回答5:


A construct like x = x++; indicates you're probably misunderstanding what the ++ operator does:

// original code
int x = 7;
x = x++;

Let's rewrite this to do the same thing, based on removing the ++ operator:

// behaves the same as the original code
int x = 7;
int tmp = x; // value of tmp here is 7
x = x + 1; // x temporarily equals 8 (this is the evaluation of ++)
x = tmp; // oops! we overwrote y with 7

Now, let's rewrite it to do (what I think) you wanted:

// original code
int x = 7;
x++;

The subtlety here is that the ++ operator modifies the variable x, unlike an expression such as x + x, which would evaluate to an int value but leave the variable x itself unchanged. Consider a construct like the venerable for loop:

for(int i = 0; i < 10; i++)
{
    System.out.println(i);
}

Notice the i++ in there? It's the same operator. We could rewrite this for loop like this and it would behave the same:

for(int i = 0; i < 10; i = i + 1)
{
    System.out.println(i);
}

I also recommend against using the ++ operator in larger expressions in most cases. Because of the subtlety of when it modifies the original variable in pre- versus post-increment (++x and x++, respectively), it is very easy to introduce subtle bugs that are difficult to track down.




回答6:


According to Byte code obtained from the class files,

Both assignments increment x, but difference is the timing of when the value is pushed onto the stack

In Case1, Push occurs (and then later assigned) before the increment (essentially meaning your increment does nothing)

In Case2, Increment occurs first (making it 8) and then pushed onto the stack(and then assigned to x)

Case 1:

int x=7;
x=x++;

Byte Code:

0  bipush 7     //Push 7 onto  stack
2  istore_1 [x] //Pop  7 and store in x
3  iload_1  [x] //Push 7 onto stack
4  iinc 1 1 [x] //Increment x by 1 (x=8)
7  istore_1 [x] //Pop 7 and store in x
8  return       //x now has 7

Case 2:

int x=7; 
x=++x;

Byte Code

0  bipush 7     //Push 7 onto stack
2  istore_1 [x] //Pop 7 and store in x
3  iinc 1 1 [x] //Increment x by 1 (x=8)
6  iload_1  [x] //Push x onto stack
7  istore_1 [x] //Pop 8 and store in x
8  return       //x now has 8
  • Stack here refers to Operand Stack, local: x index: 1 type: int



回答7:


It's incremented after "x = x++;". It would be 8 if you did "x = ++x;".




回答8:


The incrementing occurs after x is called, so x still equals 7. ++x would equal 8 when x is called




回答9:


When you re-assign the value for x it is still 7. Try x = ++x and you will get 8 else do

x++; // don't re-assign, just increment
System.out.println(x); // prints 8



回答10:


Post Increment operator works as follows:

  1. Store previous value of operand.
  2. Increment the value of the operand.
  3. Return the previous value of the operand.

So the statement

int x = 7;
x = x++; 

would be evaluated as follows:

  1. x is initialized with value 7
  2. post increment operator stores previous value of x i.e. 7 to return.
  3. Increments the x, so now x is 8
  4. Returns the previous value of x i.e. 7 and it is assigned back to x, so x again becomes 7

So x is indeed increased but since x++ is assigning result back to x so value of x is overridden to its previous value.




回答11:


because x++ increments the value AFTER assigning it to the variable. so on and during the execution of this line:

x++;

the varialbe x will still have the original value (7), but using x again on another line, such as

System.out.println(x + "");

will give you 8.

if you want to use an incremented value of x on your assignment statement, use

++x;

This will increment x by 1, THEN assign that value to the variable x.

[Edit] instead of x = x++, it's just x++; the former assigns the original value of x to itself, so it actually does nothing on that line.




回答12:


What happens when int x = 7; x = x++;?

ans -> x++ means first use value of x for expression and then increase it by 1.
This is what happens in your case. The value of x on RHS is copied to variable x on LHS and then value of x is increased by 1.

Similarly ++x means -> increase the value of x first by one and then use in expression .
So in your case if you do x = ++x ; // where x = 7
you will get value of 8.

For more clarity try to find out how many printf statement will execute the following code

while(i++ <5)   
  printf("%d" , ++i);   // This might clear your concept upto  great extend



回答13:


++x is pre-increment -> x is incremented before being used
x++ is post-increment -> x is incremented after being used

int x = 7; -> x get 7 value <br>
x = x++; -> x get x value AND only then x is incremented



回答14:


So this means: x++ is not equal to x = x+1

because:

int x = 7; x = x++;
x is 7

int x = 7; x = x = x+1;
x is 8

and now it seems a bit strange:

int x = 7; x = x+=1;
x is 8

very compiler dependent!




回答15:


x = x++;

This is the post-increment operator. It should be understood as "Use the operand's value and then increment the operand".

If you want the reverse to happen i.e "Increment the operand and then use the operand's value", you must use the pre-increment operator as shown below.

x = ++x;

This operator first increments the value of x by 1 and then assigns the value back to x.




回答16:


I think this controversy can be resolved without going into code & just thinking.

Consider i++ & ++i as functions, say Func1 & Func2.

Now i=7;
Func1(i++) returns 7, Func2(++i) returns 8 (everybody knows this). Internally both the functions increment i to 8 , but they return different values.

So i = i++ calls the function Func1. Inside the function i increments to 8, but on completion the function returns 7.

So ultimately 7 gets allocated to i. (So in the end, i = 7)




回答17:


This is because you used a post-increment operator. In this following line of code

x = x++;

What happens is that, you're assigning the value of x to x. x++ increments x after the value of x is assigned to x. That is how post-increment operators work. They work after a statement has been executed. So in your code, x is returned first after then it is afterwards incremented.

If you did

x = ++x;

The answer would be 8 because you used the pre-increment operator. This increments the value first before returning the value of x.



来源:https://stackoverflow.com/questions/12033676/if-y-1-and-y-y-why-when-i-print-y-is-the-value-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!