The self-increment operator and the intermediate cache variable mechanism in Java

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/maggiedorami/article/details/7986098

Let's look at a program like this:

  1. Public static void Main (string[] args) {
  2. int i, SUM1, sum2;
  3. i=0;
  4. Sum1 = (i++) + (i++);
  5. System.out.println ("sum1=" +sum1);
  6. i = 0;
  7. sum2 = (++i) + (++i);
  8. System.out.println ("sum2=" +sum2);
  9. }

The result of this operation is:

[Java]View Plaincopy
    1. sum1=1
    2. sum2=3


And I write the same logic code in C, and I get different results:

[CPP]View Plaincopy
    1. void Main ()
    2. {
    3. int i,sum1,sum2;
    4. i=0;
    5. Sum1= (i++) + (i++);
    6. printf ("sum1=%d\n", sum1);
    7. i=0;
    8. Sum2= (++i) + (++i);
    9. printf ("sum2=%d\n", sum2);
    10. GetChar ();
    11. }

The result of this operation is:

[CPP]View Plaincopy
    1. Sum1=0
    2. Sum2=4

This differs because in C, each variable can have only one unique value at each point in its life cycle. As a result, the contents of the corresponding memory area of the variable are rewritten at each increment operation.

In Java, sum1= (i++) + (i++) is executed, and 2 temporary integer variable objects are created to store the results of each self-increment operation.

Java uses the mechanism of this intermediate cache variable.

Then look at the programmer interview a very classic example:

[Java]View Plaincopy
    1. Public static void Main (string[] args) {
    2. Int J = 0;
    3. For (int i = 0; i < ; i++)
    4. J = j + +;
    5. System.out.println (j);
    6. }

For Java, the output value of j is 0.

Because the middle cache variable mechanism of Java causes the j=j++ statement to be decomposed into the following actions:

[Java]View Plaincopy
    1. temp = j;
    2. j = j + 1;
    3. j = temp;

Individuals feel that this use of self-growth is not good, should be in complex statements to avoid the use of post-increment (self-subtraction).

In addition, it is worth noting that some languages that use the mechanism of intermediate cache variables, the output is not necessarily 0. For example, when C + + has a post-increment operation on some basic types and pointer types, the compiler will omit intermediate cache variables.

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.