We know that there are two purposes for defining variables in Java:
1. Prevent the garbage collection mechanism from being recycled, after all, if the ' code ' is not explicitly pointing to real physical memory, it is likely to be garbage collected by the Java garbage collection mechanism.
2. Easy to reference and easy to handle.
1 PackageTESTP1;2 3 Public classTest01 {4 Public Static voidReplace1 (inta) {5A = 10;6 }7 Public Static intReplace2 (inta) {8A = 10;9 returnA;Ten } One Public Static voidReplace3 (String s) { As = "a"; - } - Public Staticstring Replace4 (string s) { thes = "a"; - returns; - } - + Public Static voidMain (string[] args) { - intA = 0; + Replace1 (a); ASystem.out.println ("in replace1:a=" +a); atA =Replace2 (a); -System.out.println ("in replace2:a=" +a); -String s = "Make"; - Replace3 (s); -System.out.println ("in replace3:s=" +s); -s=Replace4 (s); inSystem.out.println ("in replace4:s=" +s); - } to}
Local variables and external variables
In the Replace1 of the above code, the parameter A is the basic data type, which points to the physical memory where the number 10 is located, and does not change the physical address that the external variable a points to when the method is called. The output does not change the value of the variable, but if we call the method of the int return type REPLACE2 we find that its value is changed because we assign the physical address of the internal variable to the external variable A, in the method Replace3 of the reference data type that returns the value void, The result of the method is that the form parameter refers to the object A, also only within the method of the function, out of the method, the variable is the original reference object, in the return type is a string type, we also let the external variable point to the physical memory of object A, the value of course will change.
Discussion on the problems related to the transfer of method parameters in Java