Passing the parameters of the Java method
Basic Type
The parameter passing of Java methods is sometimes not easy to understand, and some analysis is carried out based on simple graphics. For more information, see <Java Employment Training tutorial>.
This article first looks at the transmission of parameters of the basic type.
The following code is used as an example:
Public class passvalue {
Public staticvoid main (string [] ARGs ){
Int x = 5;
Change (X );
System. Out. println (X );
}
Public staticvoid change (int x ){
X = 3;
}
}
The parameter transfer process during method Runtime is to first open up a space for the variable of the defined type in the method area, and the parameter variable (that is, the local variable in the method) points to this space. See Figure 1_1.
Then, assign the value of the real parameter to this local variable. In this case, the local variable X in the method is assigned to 5. See Figure 1_2.
At this point, the transfer of the call method change parameter is complete. Check the code running status. In the method, the value of X of the local variable is 3, but the value of X outside the method is 5. See Figure 1_3.
The change method ends. The memory occupied by X in the change method is released and the program returns the result. The variable X in the main method is still 5 and has not changed. See Figure 1_4.
At this point, the entire program is executed.
Through the image, you can clearly see the variable transfer process.