In Java:
Using the intermediate variable mechanism:
i = i++;
Equivalent:
temp = i;
i = i + 1;
i = temp; (i.e. i = i++ = temp)
Note: Generate a self-increment instruction before the two-dollar operator ("=", "+") generates a custom command (that is, it has been self-increasing before the "+" and "=" operations have been executed). ##
The essence of the problem: in Java, when you perform a self-increment operation, a temporary variable is assigned to each of the self-increment operations, and if it is a prefix plus (++i), it will be assigned after 1 (to the temporary variable), and if it is a suffix plus (i++), it will be "first assigned (to the temporary variable) and The final use of the operation is not the variable itself, but the temporary variable assigned the value.
=================================================================================================
int a = 0,sum;
In Java, after using the expression a++, a immediately adds one;
For sum = a++ + a++;
Java: After the first a++ executes, the value of a is immediately added to 1, and the value of a is 1 before the second a++ executes.
A = 0;
A = a++ + a++;
Temp1 = A; --Temp1 = = 0;
A = a+1;
Temp2 = A; --Temp2 = = 1;
A = a + 1;
A = Temp1 + Temp2;
=================================================================================================
int a = 0, sum;
A = a++;
System.out.println (a);
The result is: 0 Note: The value of variable A is changed from 0 to 1 to 0 (initialized to 0--and a from 1--and assigns the value of the TEMP variable to a)
--------------------------------------------------------------------------------------------------------------- ---------------------------
int a = 0, sum;
sum = a++ + a++;
SYSTEM.OUT.PRINTLN (sum);
System.out.println (a);
The result is: 1 and 2
--------------------------------------
int a = 0, sum;
A = a++ + a++;
System.out.println (a);
The result is: 1
--------------------------------------
int a,sum;
A = 0;
sum = a++ + ++a;
SYSTEM.OUT.PRINTLN (sum);
System.out.println (a);
The result is: 2 and 2
=============================================================================================================== =
In the C language:
+ + is a unary operator: + and = Both are two-tuple operators
For unary operators, the return value is: i++ is the value of I, ++i is the value of I plus one
The i++ operation means that a self-increment instruction is generated after the instruction of the two-dollar operator is generated (that is, after the "+" and "=" operations have been completed, the increment is added).
The ++i operation means that a self-increment instruction is generated before the instruction generated by the two-dollar operator (that is, it is self-increasing before the "+" and "=" operations are executed).
=============================================================================================================== =
int i = 0, sum;
Test unary operators
If printf ("%d", I); The result is: 0
If printf ("%d", i++); The result is: 0
If printf ("%d", ++i); The result is: 1
----------------------------------------------------------------------
int i = 0, sum;
Testing the two-dollar operator
sum = i++;
printf ("%d", sum);
printf ("%d", I);
The result is: 0 and 1
If you change to:
i = i++;
printf ("%d", I);
The result is: 1
----------------------------------------------------------------------
int a = 0, sum;
sum = a++ + a++;
printf ("%d\n", sum);
printf ("%d\n", a);
The result is: 0 and 2
Analysis:
First a++ is executed (at this point, the value of a is 0),
Then execute the second a++ (at which point A has a value of 0),
Finally, the addition and assignment operations are performed (at this point a value of 0,sum is 0 + 0 = 0),
After the addition operation is finished, perform a two plus action (at which point A is 2)
If you change to:
A = a++ + a++;
printf ("%d", a);
The result is: 2
----------------------------------------------------------------------
int a = 0,sum;
A = a++ + ++a;
printf ("%d\n", a);
Process:
First, a self-increment of 1, which is brought by ++a (even if a++ on the far left). At this point a = 1
We then calculate the two operand of the + sign.
Because they are actually a (this involves the compiler implementation problem, most C compilers will combine the two operands, that is, the variable a),
The value is 1, so that the return value of a++ + ++a is 1+1=2, and then the value of a++ + ++a is assigned to a, when a = 2
Finally, complete the self-increment instruction brought by a++, at which point A is the value of 2+1=3, which is a = 3
The result is: 3
If you change to:
sum = a++ + ++a;
printf ("%d", sum);
printf ("%d", a);
Process:
First, a self-increment of 1, which is brought by ++a (even if a++ on the far left). At this point a = 1
Then, we calculate the + number of the two operands.
Because they are actually a (this involves the compiler implementation problem, most C compilers will combine the two operands, that is, the variable a),
The value is 1, so that the return value of a++ + ++a is 1+1=2, and then the value of a++ + ++a is assigned to sum, when sum = 2,a = 1
Finally, complete the self-increment instruction brought by a++, at which point A is the value of 1+1=2, which is a = 2
Results are: 2 and 2
=============================================================================================================== =
A = a++ ++a; Differences in Java and C
In Java, a++ is executed first,
C, first execute the ++a
The difference between i++ in Java and i++ in C