Correct understanding of I = I ++, and correct understanding of I

Source: Internet
Author: User

Correct understanding of I = I ++, and correct understanding of I

I = I ++ has been frequently encountered in the recent written examination. This problem is still relatively simple, but it doesn't matter if I + = I ++ doesn't matter, I saw a big problem.

Common sense

1. result + = expression is equivalent to result = result + expression in all languages.

2. int I = B ++; how is this done? First, the system copies the value of B in the memory stack to the Register, then, write the copy value + 1 to.

Enter the subject

The following describes the execution of C and java languages.

First, let's take a look at this code.

int i=0;i=i++;

C: 1 java: 1

Let's look at this code. What do you think is the I value?

int i=0;i+=i++;

C: 1 java: 0

Finally, let's take a look at this

int i=0;i+=i++ + i++ 

C: 3 java: 1

What do you feel now? I was so aggressive at the beginning. Why is this happening? Let's explain it slowly.

 

First look at the first situation

I = I ++ is actually from first copying the I value from the stack to the register, then the addition operation will be executed, and the value before the addition operation will be returned, so the result is 0, in fact, it is equivalent to the following functions:

int mockAdd(int &i){ int temp=i; i=i+1; return temp;}

This is unified in C and java, and I believe everyone understands it well.

 

Let's look at the second situation.

I + = I ++ can be understood as I = I + I ++, which is also consistent between the two languages, but why is C language equal to 1 and java equal to 0?

C language is better understood:

This operation first executes I ++. At this time, the value of I is 1. This operation returns 0, and the computation expression is changed to I = I + 0.

Then, the result of I = 1 + 0 is 1.

How to Understand the java language? Let's look at the java bytecode file.

Public class Test1 {public static void main (String [] args) {int I = 0; I ++ = I ++ ;}} corresponding to the bytecode core file public static void main (java. lang. string []); Code: 0: iconst_0 // defines a constant 0 1: istore_1 // save 0 to variable 1. Here the variable is our I 2: iload_1 // read the I value to the register 3: iload_1 // read the I value to the Register 4: iinc 1, 1 // execute the ++ operation 7: iadd // execute addition operation 8: istore_1 // Save the operation result to variable 1 9: return // return}

Obviously, when I = 0, java performs the I = I + = process, the I values on both sides of the left and right are obtained before the computation, so the actual operation is I = 0 + 0 ++. Isn't the answer 0?

 

Let's look at the third case.

I + = I ++ is equivalent to I + I ++. Based on the left-to-right relationship and the priority relationship, the following execution process can be obtained:

Execute I = I + 0 ++ I ++ first. After execution, I = 1 and the expression changes to I = I + 0 + I ++

Then run I = I + 1 + 1 ++. After the execution, the I = 2 expression is changed to I = I + 1.

The last step is to execute I = 2 + 1 and the result is 3,

How to Understand the java language, or bytecode, as follows:

Public class Test2 {public static void main (String [] args) {int I = 0; I ++ = I ++ ;}} // The core bytecode is as follows: public static void main (java. lang. string []); Code: 0: iconst_0 // defines a constant 0 1: istore_1 // store 0 to variable 1, that is, our I 2: iload_1 // read the I value to register 3: iload_1 // read the I value to register 4: iinc 1, 1 // perform auto-increment operation 7: iload_1 // read the I value to register 8: iinc 1, 1 // perform the auto-increment operation 11: iadd // perform the addition operation 12: iadd // perform the addition operation 13: istore_1 // associate the Operation Association variable I 14: return // return}

We can see that when I = 0, I = I + I ++ is actually the first execution of I = 0 + 0 ++ I ++ and the last I = 1 ++ answer is 0

Final Summary

1. The priority of the C language arithmetic expression Operation Command is higher than that of the value copy

The arithmetic expression operation instruction of java language is lower than the value copy priority.

2. Example

Int I = a + B ++. The C language will first copy the value of B to the register and execute the ++ operation before getting the value of B.

Int I = a + B ++. C will first copy the value of a to the register, then copy the value of B to the register, and then add the copied value after the ++ operation.

 

 

Related Article

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.