Java Programmer Interview Book 5.2.3--intermediate cache variable mechanism

Source: Internet
Author: User

See the third edition of 5.2 of Example 4, at the outset simply accept the mechanism of the intermediate cache variables, examples are as follows:

Public   class   Test {public   static   void   main (string[]   args)   {int   j   =   0       ; for   (int   i   =   0;   I   <   ;   i++)   {J   =   
the above results are 0. The explanations are as follows:

1. Temp=j;2. J=j+1;3. J=temp;

See the question in Example 5: Int j = 0; J =++j + j + + +j++ + j + +; The result equals 7, wondering, how is this not the "mechanism of the intermediate cache variable" last j=0?

Read some of the explanations are not very clear. Under consideration, the mechanism of "intermediate cache variables" was found to be the calculation of a single expression. For example, topic 5 can be decomposed into:

1. J = j+1;            (++j) self-increment 2. Temp1 = J =1;       (++J) The result of an expression with an intermediate cache variable of 3. Temp2 =  J =1;      (j + +) expression results in an intermediate cache variable of 4. j = j+1=2;          (j + +) self-increment 5. Temp3 =  J =2;      (j + +) expression results in an intermediate cache variable of 6. j = j+1=3;          (j + +) self-increment 7. Temp4 =  J =3;      (j + +) expression results in an intermediate cache variable of 8. j = j+1=4;          (j + +) self-increment 9. j = temp1 + Temp2 + Temp3 + temp4 = 1+1+2+3=7;

It is clear from the above decomposition that for the self-increment operation ++j and J + +, because of the different order of execution of addition one, there is an intermediate cache variable in Java to store the value of its individual expression, and the result of J's self-increment is still in the original variable storage area. Because the body is the value of J, and the value of a single expression is a temporary variable value produced in the middle, it is a value that can be discarded after the end of the entire calculation expression, so a temporary intermediate cache variable is placed. This can be achieved by the increment decrement operation in the calculation of the value of 1 order difference between the expression and the difference between the main body values two variables stored!

Thinking about this mechanism should also be a deliberate consideration for Java designers. As a program designer, when an expression of J=f (j) is present, it must be the final value of the entire expression of F (j), rather than the value of J being increased by various self-decrement in the process (if so, that f (*) is meaningless, rather than simply preserving the various self-increment of J and throwing away other subtraction expressions), So the value of F (j) cannot be covered by the various additions and deletions of J, so there is a buffer to retain the original intention of the designer in the expression. However, as a program designer, this situation is still as few as possible, the more simple and intuitive program is bound to be the higher quality, the more fault-tolerant!

Some interview topics Although not very advanced, but still need to carefully ponder, rote learning is not to get a real promotion, refueling!










Java Programmer Interview Book 5.2.3--intermediate cache variable mechanism

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.