When calling a method, you need to input appropriate parameters according to the method declaration. Passing parameters each time you call the method greatly enhances the uniformity of the method and avoids repeated functional code within the method. However, if the parameter value is modified inside the method when a parameter is actually passed, does the variable used for calling change?
For example, the following code:
/**
* Sample code for passing Parameters
*/
Public class transfervaluedemo {
Public static void main (string [] ARGs ){
Int M = 10;
Int [] A = {1, 2, 34 };
Test (M, );
System. Out. println (m );
System. Out. println (A [0]);
}
Public static void test (int n, int [] T ){
N = 0;
T [0] = 123;
}
}
After the program is executed, the output result of the program is: 10 123. When the test method is called, it is also an input parameter. Why does the value of Variable M not change while the value of a [0] has changed? The following describes the problem.
When passing parameters, there are generally two rules for passing parameters. The same is true in Java. The two methods are as follows:
L pass by value (by value)
Passing by value means that each time a parameter is passed, the original value of the parameter is copied to a new one, and the new value is passed to the method. When the method is modified, the modified value is copied, but the original value is not changed.
Note: The original values of parameters transmitted in this mode are not changed.
L by address)
Address-based transmission means that each time a parameter is transferred, the storage address of the parameter in the memory is passed to the method, and the content of the corresponding storage area is changed through the storage address within the method. Since there is only one fixed address value in the memory, the original value of the parameter changes after the parameter value is modified in the method.
Note: When you modify the parameter value in a method, the original value of the parameter is also changed.
In Java, for those data types that are passed by value and those data types that are passed by address, hard rules are made as follows:
L data types passed by value: eight basic data types and strings (this can be understood, but in fact, string is also the transmitted address, but the string object is different from other objects, A String object cannot be changed. A new object is generated when the content is changed. Then the stringbuffer can be used, but it only changes its content. The memory address pointed to by the external variable cannot be changed. For the special characteristics of string, see the in-depth analysis of the string class in this blog ).
L address-based data types: All composite data types except string, including arrays, classes, and interfaces
According to the syntax rules, the variable M in the above Code is of the int type, which is passed by value. Therefore, when the parameter value is modified inside the method, the m value will not change, the type of A is array, which is passed by address. Therefore, when the parameter value is modified inside the method, the original value is changed.
You must be careful when transferring values by value or by address in actual use, especially when you need to modify the parameter values inside the method. In some cases, you do not want to modify the value of a parameter that is passed by value, or you do not want to modify the value of a parameter when passing by address. The following is the sample code for implementing these two methods.
When passing by value, modify the parameter value through the return value:
/**
* Modify the value of a parameter through the return value of the type passed by value
*/
Public class transfervaluedemo1 {
Public static void main (string [] ARGs ){
Int M = 10;
M = test1 (m); // manually assign a value
System. Out. println (m );
}
Public static int test1 (int n ){
N = 15;
Return N;
}
}
In this sample code, the modified parameter n is returned to assign a value to the variable M, and the value of passing parameters by value is forcibly modified to correct the parameter value.
When passing by address, you can regenerate the variable to avoid modifying the parameter value:
/**
* When passing by address, you can regenerate the variable to avoid modifying the parameter value.
*/
Public class transfervaluedemo2 {
Public static void main (string [] ARGs ){
Int [] A = {1, 2, 3 };
Test2 ();
System. Out. println (A [0]);
}
Public static void Test2 (INT [] m ){
Int [] n = new int [M. Length];
For (INT I = 0; I <M. length; I ++ ){
N [I] = m [I];
}
N [0] = 10;
}
}
In this sample code, you can create an array by creating an internal method, and assign the value of each parameter in the input array to the newly created array to copy the array content, then, when the value in the copied array is modified, the original parameter content will not change.
Here, the system introduces the rules for passing parameters in the Java language. A deep understanding of these rules will allow for more flexible program design. For example, you can easily return multiple parameters by using the address-based feature of the composite data type. The code example is as follows:
Public int test3 (INT [] M, int [] n ){......}
In this method, three values are actually returned, the return value of one int, the value of array m, and the value of array n. This is only a basic usage of parameter passing, this method is also widely used in the API documentation provided by JDK.
//////////////////////////////////////// /////////////////////////////////////
When it is an object, the method obtains the referenced copy. Swap (obj a, obj B)
The call is swap (c, d), that is, the reference of objects A and B is copied. References A and C point to the same object, and references B and D point to the same object. If there is no problem with the setvalue Method for integer, but it is a pity that no value can be obtained using int T = P. intvalue, but it still cannot be exchanged. If a new object is created, only C and D are affected, and A and B are not affected.
Note the difference between Java and VC. Java has no pointer, so no matter how the form parameters in the method change, the real parameters outside the method are not affected. (This sentence is not completely correct)
The string object is different from other objects. The String object cannot be changed, and a new object is generated when the content is changed. Then the stringbuffer can be used, but it only changes its content. The memory address pointed to by the external variable cannot be changed.