Java parameters and references are generic. Although Java has no reference concept When passing in parameters in Java, the actually passed parameter object is the reference of the external object, that is, re-generate an object pointing to the external object, but the two objects point to the same memory object. For example, we will illustrate the relationship between parameters and references. We assume that the changestring method is Public void changestring (string ){ A = new string ("I am B! "); System. Out. println (); } When the changestring (string a) method is called as follows: String A = new string ("I Am! "); Changestring (); ...... Java practices are equivalent String A = new string ("ABC "); String B =; Changestring (B ); ............ That is to say, within the called method, the input parameter object and the external object are different objects;Although the two point to the same memory object, it is easy to disconnect the two. For example, we can use the following method to test String A = new string ("I Am! "); System. Out. println (); // Call the changestring Method Changestring (); System. Out. println (); The result should be I am !; I am B !; I am !; Also That is to say, the value of external object A has not changed, because in changestring (string ) Inside the method, parameter object A is not external object A (in fact, if you change the method to public void changestring (string B) in this way, we can better understand that parameter object B and External Object A are two objects ),Parameter object A and External Object A are two objects, pointing to the same memory object new string ("ABC"), but inside the method, when parameter object A is re-assigned (A = new string ("B"), the parameter object A is redirected to a new memory object new string ("I am B! "). In this way, the parameter object A is disconnected from the External Object. So the value of external object a cannot be changed. Through the above instructions, you should understand the parameters and references. Here,Pay attention to the value assignment of variables. When "=" is used to assign values to variables, it is generally to re-specify a new memory object for variable A. Therefore, when"Read-only object "As a parameter, the value of the external object is safe. These objects and data include
Basic types, such as int and char Read-Only objects, such as string and integer That is to say, you cannot try to change the value of the original object by calling the method. can you exchange the values of A and B by calling changevalue (int A, int B? In some cases, if you need to change the value of the original object in the method, the method is very simple. Do not point the parameter object to a new internal object. On this basis, call the method of the parameter object to change the memory object. For example, consider the following method: Public void changebuffervalue (stringbuffer SB ){ SB. append ("I am B! "); } Call Stringbuffer A = new stringbuffer ("I Am! "); System. Out. println (A. tostring ()); Changebuffervalue (); System. Out. println (A. tostring ()); The input result should be I am! I am! I am B! If the method is written Public void changebuffervalue (stringbuffer SB ){ SB = new stringbuffer ("I am B! "); } You should know what the result is !, Therefore, when you expect the method to change the value of the input variable, use the "=" number as the parameter object to assign a value again. 1. For the original data type, that is, Int, long, Char, and other types, it is a value transfer. If you modify the value in the method, after the method call ends, the value of that variable is not changed. 2. For the object type, that is, the object subclass, If you modify the value of its member in the method, the modification takes effect. After the method call ends, its members are new values, but if you point it to another object, after the method call ends, the original reference to it does not point to the new object.
CodeAs follows: Public class tester { Public static void main (string [] ARGs ){ Int primitive = 2; Changeprimitive (primitive ); // The primitive value is still 2 Myclass = new myclass (); Changeobject (myclass ); // Myclass still points to the object before changeobject execution // But myclass. I is equal to 3 } Public static void changeprimitive (INT primitive ){ Primitive = 3; } Public static void changeobject (myclass ){ Myclass. I = 3; Myclass = new myclass (); } } Class myclass { Int I; } For remote calls, no matter what type it is, after the call is completed, the input parameters are not changed (of course, the future is to directly call the remote method, if other proxy or facade classes are passed in the middle, the classes do not need to be modified ). As for calling through the locale interface, I am not sure whether it is a remote call. Later, confirm and update again. |