Differences between I ++ in java and I ++ in C

Source: Internet
Author: User

Differences between I ++ in java and I ++ in C

In java:

Using the intermediate variable mechanism:
I = I ++;

Equivalent:

Temp = I;
I = I + 1;
I = temp; (I = I ++ = temp)

# Note: An auto-increment command (I .e: before the "+" and "=" operations are executed, they are automatically added ). ##

Problem: in java, a temporary variable is allocated for each auto-increment operation when performing the auto-increment operation. If it is a prefix plus (++ I ), "First add 1 and then assign value (to the temporary variable)". If it is a suffix add (I ++), it will "assign value first (to the temporary variable) and then add 1 ". The final operation is not a variable, but a temporary variable assigned a value.

========================================================== ========================================================== ========================


Int a = 0, sum;

In java, after the expression a ++ is used, a immediately adds one;


For sum = a ++;

In java: after the first a ++ execution is complete, the value of a is immediately added to 1. Before the second a ++ execution, the value of a is 1.

A = 0;
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 ();

Result: 0. Note: The process of changing the value of variable a is as follows: 0 --> 1 --> 0 (Initialization is 0 --> a auto-increment is 1 --> assign the value 1 of the temporary variable to)
Certificate ------------------------------------------------------------------------------------------------------------------------------------------

Int a = 0, sum;

Sum = a ++;

System. out. println (sum );
System. out. println ();

Result: 1 and 2

--------------------------------------
Int a = 0, sum;

A = a ++;

System. out. println ();

Result: 1

--------------------------------------
Int a, sum;
A = 0;

Sum = a ++;

System. out. println (sum );
System. out. println ();

Result: 2 and 2


========================================================== ========================================================== ======================================

C language:

++ Is a unary operator: + and = are both binary operators.

For The unary operator, the return value is: I ++ is the value of I, and ++ I is the value after I plus.

The I ++ operation means that an auto-increment command is generated after the command corresponding to the binary operator is generated (that is, the auto-increment command is generated after the "+" and "=" operations are completed ).

The ++ I operation means that an auto-increment command is generated before the command corresponding to the binary operator is generated (that is, the auto-increment command is generated before the "+" and "=" operations are executed ).

========================================================== ========================================================== ======================================


Int I = 0, sum;

// Test The unary operator

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;

// Test the binary operator

Sum = I ++;

Printf ("% d", sum );
Printf ("% d", I );

Result: 0 and 1

If changed:

I = I ++;
Printf ("% d", I );

The result is: 1

----------------------------------------------------------------------

Int a = 0, sum;


Sum = a ++;

Printf ("% d \ n", sum );
Printf ("% d \ n", );

Result: 0 and 2

Analysis:

First execute the first a ++ (at this time, the value of a is 0 ),

Then execute the second a ++ (in this case, the value of a is 0 ),

Finally, perform addition and value assignment (in this case, the value of a is 0, and the value of sum is 0 + 0 = 0 ),

After the addition operation is complete, execute the two add-on action of a (the value of a is 2)

If changed:

A = a ++;

Printf ("% d", );

The result is: 2

----------------------------------------------------------------------

Int a = 0, sum;

A = a ++;
Printf ("% d \ n", );

Process:
First, a increases by 1, which is brought by ++ a (even if a ++ is on the far left ). At this time, a = 1

Then, we calculate the two operands of the plus sign.
Because they are actually all a (this involves the implementation of the compiler, most C compilers will merge these two operands, that is, all are variable ),
In this case, the return value of a ++ a is 1 + 1 = 2, and then the value of a ++ a is assigned to, at this time, a = 2

Finally, complete the auto-increment command brought by a ++. The value of a is 2 + 1 = 3, which is a = 3.

Result: 3


If changed:
Sum = a ++;
Printf ("% d", sum );
Printf ("% d", );


Process:
First, a increases by 1, which is brought by ++ a (even if a ++ is on the far left ). At this time, a = 1

Then, we calculate the two operands of the plus sign.
Because they are actually all a (this involves the implementation of the compiler, most C compilers will merge these two operands, that is, all are variable ),
In this case, the return value of a ++ a is 1 + 1 = 2, and then the value of a ++ a is assigned to sum, sum = 2, a = 1

Finally, complete the auto-increment command brought by a ++. At this time, the value of a is 1 + 1 = 2, which is a = 2.

Result: 2 and 2

========================================================== ========================================================== ======================================

A = a ++ a; Difference between java and C

In java, execute a ++ first,
In C, execute ++ a first

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.