[Javase Learning note]-6.6 The transfer process of the basic data type and reference data type parameters

Source: Internet
Author: User

This section is the basic data type parameters and the pass-through process for reference data type parameters.


Data type parameters and reference parameters we've covered in the previous chapters, so let's take a look at the following two-segment code:

Basic data type parameters pass class Demo{public static void Main (string[] args) {int x = 3;change (x);//Call Method System.out.println ("x =" + x);// }public static void change (int x) {x = 4;}} Reference type data parameters Pass class Demo{int x = 3;public static void Main (string[] args) {Demo d = new Demo ();d. x = 9;change (d); System.out.println ("d.x =" + d.x);} public static void Change (Demo d) {d.x = 4;}}
Now let's analyze each of these two code execution routines separately.

One, for the basic data type parameter pass code execution process analysis:

1.main method into the stack memory, the main method has the basic data type variable int x;

2. Assign a value of 3 to the variable x in the main method;

3. Call the Change (x) method, the change method into the stack;

4. Assign a value of 4 to the Change method variable x;

5. Jump out of the change method. At the same time, the change method out of the stack, the release of the whole method and the method of the X, that is, the x=4 release;

6. Run the print statement. In the stack, there is only x in the main method, then the printed x=3;

7. Jump out of the main method. End the program.

Let's see if the printing results are consistent with our analysis?


Second, for reference data type parameter pass code execution process analysis:

1.main method into the stack memory. The main method has a class type variable demo D;

2.new creates a Demo object that opens up a space in the heap memory. and pass the space address to D (if we are 0x0078 here), and initialize the x in the address to 0, and then assign the 3 to x;

3. Assign x in the heap memory (0x0078) of D as 9;

4. Call the Change (d) method. Change method into the stack. The object D in the Change method is the D in the main method, pointing to the previous heap memory address (0x0078).

5. Assign x in the heap memory (0x0078) of D as 4;

6. Jump out of the change method, the same time the change method out of the stack, release the Change method and Object D in the method.

7. Run the print statement. In the stack, there is only the main method. and d points to the heap memory (0x0078), and the x in that address is the value in step 5 of 4;

8. Jump out of the main method. End the program.

Let's look at the results:


The results we printed out of the two codes show that the results are consistent with our analysis.

The process of basic data type parameters and reference data type parameters is the process we analyze above.


[Javase Learning note]-6.6 The transfer process of the basic data type and reference data type parameters

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.