Java parameter delivery

xiaoxiao2021-03-06  38

The parameter transfer mechanism in Java has always argued that "passing the value" or "inventory (pass reference)", the two sides of the debate, each other, nothing. Not only the "rookie", some of the mist, some "old birds" only know the results but they can't come. I believe that you will understand after reading the content below.

First see the basic type as a parameter transmission: public class test1 {public static void main (string [] args) {int N = 3; system.out.println ("Before change, n =" n); ChangeData (N System.out.Println ("After ChangeData (N), N =" N);} Public Static Void Changedata (INT NN) {nn = 10;}} I think this example is that everyone understands, basic type as a parameter When passing, it is the copy of the transfer value. No matter how you change this copy, the original value will not change, the output of the output proves this: Before change, n = 3AFTER ChangeData (n), n = 3 So, now Let's take a look at the example as an example of parameter, which is also a place to argue. Public class test2 {public static void main (string [] args) {stringbuffer sb = new stringbuffer ("hello"); System.out.Println ("Before Change, SB =" SB); ChangeData (SB); System. Out.println ("After ChangeData (N), SB =" SB);

Public Static Void Changedata (StringBuffer strbuf) {strbuf.append ("world!");}}

First look at the output: Before change, SB = Hello After ChangeData (n), SB = Hello World! From the results, the value of SB has changed, then it can be said: The object is passed as a parameter, it is the object The reference is transmitted, and if the reference is changed within the method, then the original object is also changed. From the output result of the above example, this explanation is reasonable. Now we will change the above example: public class test3 {public static void main (string [] args) {stringbuffer sb = new stringbuffer ("hello"); system.out.println ("Before change, sb =" SB); ChangeData (SB); System.out.Println ("After ChangeData (N), SB =" SB);

Public Static Void ChangeData (StringBuffer strbuf) {strbuf = new stringbuffer ("hi"); strbuf.append ("world!");}} In accordance with the experience of the above example: When the object is passed as a parameter, the reference to the object is transmitted. If the reference is changed within the method, then the original object is also changed. You will think should be output: Before change, SB = Hello After ChangeData (N), SB = Hi World! But run this program, you will find the result: Before change, SB = HelloAfter ChangeData (n), SB = Hello This is where people confused, when the object is passed as a parameter, the value of the object is also changed in the method, why is some of the values ​​of the original object, and some did not change the value of the original object? At this time, "pass the value" or "pass reference"? Let's take a closer look at this mystery. First look at Test2: StringBuffer SB = New StringBuffer ("Hello"); After the execution is executed, a SB object will be generated in memory, please see Figure 1: As shown in Figure 1, SB is a reference Inside, an address "@ 3a" (this "@ 3a" is an example of me, representing the memory address, you just need to know whether it is a memory address), and this address is "Hello" string Address in memory. ChangeData (SB); After performing this, the SB passed SB to the StringBuffer Strbuf in the ChangeData method. Since the address is stored in SB, the same address will also be stored in the strbuf. Take you see Figure 2: At this time, SB and Strbuf are the same as the memory address stored, therefore, all points to "Hello". Strbuf.Append ("World!"); After performing this sentence in the ChangeData method, the value in the memory pointed to by StrBuf is changed, as shown in Figure 3 below: So, Test2 This program finally outputs: after ChangeData (N), SB = Hello World! Take a look at Test3. StrBuf = new stringbuffer ("hi") without execution to the ChangeData method; before the object in the memory is the same, "Figure 2" is the same, and the strbuf = new stringbuffer ("hi") is performed; After that, it turned: At this time, StrBuf is no longer point to the address of "Hello", but pointing to "Hi" address "@ 3b" (the same "@ 3b" is an example), New operator After the operation is successful, the store will always open a storage area in memory.

Strbuf.Append ("World!"); After the sentence is executed, it can be seen by the above figure. Since the storage address in SB and Strbuf is different, although the value in the memory pointed to by StrBuf changes, SB points The memory is not changed, so the following result is output: After ChangeData (n), the SB = Hellostring class is a special class, and some operators are overloaded, such as string str = " Hello "; equals String str = new string (" Hello "); string str =" Hello "; str = STR " world! "; Equivalent to str = new string (NEW STRINGBuffer (STR)). Append ("World!"))); Therefore, you will find the String object and the basic type, in general, in general, change the value in the method, and the original object will not be changed. In summary, we will understand that when the object in Java is passed as a parameter, it is a copy of the address in the memory to the parameters. You can try to analyze the results of the following example to see if the results of the running result is the same as the result of your analysis: public class test4 {public static void main (string [] args) {stringbuffer sb = new stringBuffer (" Hello "); System.out.Println (" Before Change, SB = " SB); ChangeData (SB); System.out.Println (" After ChangeData (N), SB = " SB);} Public Static Void Changedata (StringBuffer strbuf) {StringBuffer SB2 = New StringBuffer ("hi"); strbuf = sb2; sb2.append ("world!");}

} Tip: Performed Strbuf = SB2;

转载请注明原文地址:https://www.9cbs.com/read-78182.html

New Post(0)