Java increment and assignment operator

I got confused in the post ++ and pre ++ operator, for example in the following code

int x = 10; x = x++; sysout(x); 

will print 10?

He types 10, but I expected him to type 11

but when i do

 x = ++x; instead of x = x++; 

it will print eleven, as I expected, so why x = x ++; does not change the value of x?

+8
java increment post-increment pre-increment
source share
5 answers

No, listing 10 is correct. The key to understanding the reason for the result is the difference between the pre-increments of ++x and the post-increments of x++ compound assignments. When you use pre-increment, the value of the expression is taken after the increment is done. However, when you use post-increment, the value of the expression is taken before incremental and saved for later use after the result of the increment is written back to the variable.

Here is the sequence of events that leads to what you see:

  • x assigned 10
  • Due to ++ in the position after the increment, the current value of x (i.e. 10 ) is stored for later use
  • New value 11 stored in x
  • The temporary value of 10 stored in x , writing it over the 11 that was saved there.
+8
source share

Post Increment (n ++) : Follow the instructions first, then increase the value by one.

Pre Increment (++ n) : First increase the value by one, then follow the instructions.

Example:

 class IncrementTest{ public static void main(String[] args){ System.out.println("***Post increment test***"); int n = 10; System.out.println(n); // output 10 System.out.println(n++); // output 10 System.out.println(n); // output 11 System.out.println("***Pre increment test***"); int m = 10; System.out.println(m); // output 10 System.out.println(++m); // output 11 System.out.println(m); // output 11 } } 
+1
source share

I did not find the link in JLS, so this answer is based on the assumption that you are not calling undefined behavior, because it is Java.

 x = x++; // Assign the old value of `x` to x, before increment. x = ++x; // Assign the new value of `x` to x, after increment. 

This is based on the definition of postfix and prefix operators.

0
source share

Quote from Java Tutorials - Assignment, Arithmetic, and Unary Operators :

The increment / decrement operators can be applied before (prefix) or after (postfix) operands. Code result++; and ++result; will end as a result, increasing by one. The only difference is that the prefix version ( ++result ) evaluates to an incremental value, while the postfix version ( result++ ) evaluates to the original value. If you just perform a simple increment / decrement, it really doesn't matter which version you choose. But if you use this operator in terms of a larger expression, then the one you choose can make a big difference.

0
source share

When you do this:

 x = x++; 

This actually translates as "Assign" x "to" x "and then increment" x "by one."

If in this case:

 x = ++x; 

It translates into "Increment" x "by one, and then assigns it to" x ".

-one
source share

All Articles