Fun (++i, I, i++, i++) questions

Source: Internet
Author: User

This is a few months ago a study brother asked me the question, should be a C2 level on a choice question, or quite interesting, and the Internet is not the answer to the question, the title is such

public class demo{public  static void Fun (A,b,c,d) {    System.out.println (a);    System.out.println (b);    System.out.println (c);    System.out.println (d);  }  public static void Main (string[] args) {   int i=1;  Fun (++1,i,i++,i++);}  }

The output is: 4,4,2,1 must have imagined it. The calculation steps are as follows:

The first is to clarify 2 questions 1. The function is executed in a right-to-left execution of 2. The calculation results are not returned immediately after the parameter calculation, but when all the values are computed, the results are returned. As the first question: Fun (++i, I, i++, i++)//------------------------------------------- Calculation phase---------------------------------------------------1). i++; (At that time I=1) because it is i++, the value will be saved in memory and the value in the memory will be returned to the fourth parameter d=12). i++;(then i=2) because it is i++, the value is saved in memory and the value in the memory is returned to the third parameter c=23). I; (then i=3) Because it is I will not log into memory and will return I value 4). ++i; (at that time i=4) because it is the ++i program will be based on I +1 and return I value so will not be recorded in memory will be directly returned I value//------------------------------ -------------return to the calculation result stage---------------------------------------------------5). Returns the value of the memory address in the fourth parameter (1) d=16). Returns the third parameter returns 2 ) in the memory address. Returns the second parameter returns the I value b=48). Returns the first parameter returns the I value a=4

But he has a derivative question. The arguments passed in the call to the fun function in the main function become this: Fun (++i, i++, i++, ++i);

The idea of solving problems is as follows:

The second question: Fun (++i, i++, i++, ++i);//------------------------------------------- Calculation phase---------------------------------------------------1). ++i; (At that time i=2) because it is the ++i program will be on the basis of I +1 and return I value so will not be recorded in memory will directly return I value 2). i++;(at that time i=2) because it is i++, the value is saved in memory and the value in the memory is returned to the third parameter c=23). i++;(at that time I =3) because it is i++, the value is saved to memory and the value in the memory is returned to the third parameter b=34). ++i; (at that time i=5) because the ++i program will be on the basis of I +1 and return I value so will not be recorded in memory will be directly returned I value//------------- ------------------------------returns the calculated result stage---------------------------------------------------5). Returns the fourth parameter returns the I-value d=56). Return the third parameter returns 2) in the memory address of the value c=27). Returns the value b=38 in the memory address in the second parameter returns 3). Returns the first parameter returns an I value a=5

Although the instance is written in Java, the actual operating environment is actually C + +, as for why. Because vs has disassembly. This one-step tracking debugging absolute confused, the disassembly is instead at a glance.

Fun (++i, I, i++, i++) questions

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.