[Javase Learning note]-6.6 Basic data type parameter and reference data type parameter transfer procedure

Source: Internet
Author: User

This section is the basic data type parameter and the pass-through procedure of the reference data type parameter.


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:

The basic data type parameter is passed 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 parameter passed 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 the running process of these two pairs of code separately.

First, for the basic data type parameter passing code running 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, and at the same time make a stack of the method, release all the method and the X in the method, i.e. release the x=4;

6. Execute the PRINT statement, when the stack is only the X in the main method, then print out the x=3;

7. Jump out of the main method and end the program.

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


Second, for the reference data type parameter passing code running 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, opens up a space in the heap memory and passes the space address to D (which we assume as 0x0078), initializes the X in the address to 0, and assigns the 3 to X;

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

4. Call the Change (d) method, the change method into the stack, the change method in the object D is the main method of D, 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, while the change method out of the stack, release the Change method and Object D in the method;

7. Execute the PRINT statement, when the stack is only the main method, and D points to the heap memory (0x0078), the address of the X is the value in step 5 of 4;

8. Jump out of the main method and end the program.

Let's look at the results:


We can see from the results of the two codes that the results are exactly the same as our analysis.

The process of basic data type parameters and reference data type parameters, then, is the process we analyzed above.


[Javase Learning note]-6.6 Basic data type parameter and reference data type parameter transfer procedure

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.