The mechanism of intermediate cache variables in Java

Source: Internet
Author: User

On the code:
  
  1. public static void main(String[] args) {
  2. int j = 0;
  3. for (int i = 0; i < 100; i++) {
  4. j = j++;
  5. System.out.println(j);
  6. }
  7. System.out.println(j);
  8. }
If you follow CPP's understanding, the result should be 100. Java, however, is 0. Because Java is executing on the self-decrement operation, Java is executed by assigning temporary variables.
For example: j = j + +, this is done: int temp = J; j = j + 1;j = temp; Post operation is replaced after + 1.
If it is J = ++j, this is done: j = j+1; int temp = J;J = temp; the predecessor operator is replaced by the first +1.
Well, it's a rare idea to understand the intermediate cache variable substitution: int k = 0;k = ++k + k++ + k++ + k++;System. Out. println (k);
How much is the answer? O (∩_∩) o7! The detailed execution process is as follows: ++k:k = K+1;TEMP1 = k    --1
k++:       Temp2 = k    --1K = k+1;
K++:temp3 = k    --2K = k+1;
K++:temp4 = k    --3K=k+1
So:k = Temp1 + temp2 + Temp3 +temp4 = 7 statements placed in CPP are also 7.
PS, why this kind of dark topic feeling. :: >_<:: Or Python good, direct to ++--unsupport

From for notes (Wiz)

The mechanism of intermediate cache variables in Java

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.