int i=0;i=i++;i=?//Simple problem behind the simple truth
Source: Internet
Author: User
Question can you come up with an answer to this question without running? Perhaps you will not think of the answer is 1, I thought so, but the results of the operation let me very surprised, 0! Why is so? below we compare the two sections and their assembly code (JAVAP-C XXX get): int i=0; i=i++; int i=0; i=++i; -------------------------------------------------------------- ---------------------- 0:iconst_0 0:iconst_0 1:istore_2 1:istore _2 2:iload_2 2:iinc 2, 1 3:iinc 2, 1 & nbsp; 3: iload_2 4:istore_2 4:istore_2 5:return &nbsP 5:return they differ in 2, 32 sentences. from the priority view "before + +" > "=" > "after + +", so the assembly code clearly shows the process of code execution. the second piece of code results in 1, no problem. from the assembly code, = the process of assignment is to use the temporary variable to calculate = the right side of the value, and then assign to the left side of the variable. in this way, it's easy to explain the results of the first piece of code to run the: temporary variable first get I original value is 0 (assembly code 1), this time consider priority, = greater than after + +, so the temporary variable on the assignment operation , the temporary variable reads the I value first (assembly code 2), then the + + operation (assembly code 3), but the + + operation only adds one to the temporary variable itself, without affecting the original I value. According to the above ideas to analyze the second paragraph of code, it is logical:) Summary: java in the = operation by the temporary variable calculation results, and then back to the original variable, due to the priority problem will appear unexpected results , a careful analysis of the process can produce results. Forum Original: http://community.csdn.net/Expert/TopicView3.asp?id=3885309 thanks: blackhost (Jian Xin), kingfish (800 qin Chuan @ Dragon City)
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