[Javase Study Notes]-6.6 transfer process of basic data type parameters and reference data type parameters
This section describes the transmission process of basic data type parameters and reference data type parameters.
We have covered both data type parameters and reference parameters in the previous sections. Let's take a look at the following two sections of code:
// Pass the basic data type parameter class Demo {public static void main (String [] args) {int x = 3; change (x); // call the method System. out. println ("x =" + x); //} public static void change (int x) {x = 4 ;}} // pass the 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 Operation Schedules of these two pairs of codes separately.
1. Analysis of the Code passing through basic data type parameters:
1. The main method is introduced 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, and the change method is pushed to the stack;
4. assign a value of 4 to the variable x of the change method;
5. jump out of the change method, change the method out of the stack, release x in all the change methods and change methods, that is, release x = 4;
6. Execute the print statement. In some cases, only x in the main method is in the stack. Then, x = 3 is printed;
7. jump out of the main method and end the program.
Let's take a look at whether the printed results are consistent with our analysis?
Ii. Analysis of the running process of code passing by reference data type parameters:
1. The main method is added to the stack memory. The main method has a class type variable Demo d;
2. create a Demo object in new, open up a space in the heap memory, and pass the space address to d (we suppose it is 0x0078), and initialize x in this address as 0, then assign 3 to x;
3. Assign x in the heap memory (0x0078) referred to by d to 9;
4. Call the change (d) method. The change method is pushed to the stack. The object d in the change method is d in the main method, pointing to the previous heap memory address (0x0078 );
5. Assign x in the heap memory (0x0078) referred to by d to 4;
6. jump out of the change method, change the method out of the stack, and release the object d in the change method and method;
7. Execute the print statement. In some cases, the stack only has the main method, and d points to the heap memory (0x0078). In this address, x is the value 4 in step 5;
8. jump out of the main method and end the program.
Let's look at the results:
We can see from the results printed by the two codes that the results are exactly the same as our analysis.
Then, the process of basic data type parameters and reference data type parameters is the process we analyzed above.