Java parameters pass, I understand
Yuanmeng163@sz.cathay.jp
?
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 an example of parameter transmission:
Public class test1 {
?????? public static 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 changeta (int NN) {
??????? nn = 10;
???}
}
I think this example, everyone understands that when the basic type is passed as a parameter, it is a 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 = 3
After ChangeData (N), N = 3
?
So, we now come and see the object as an example of parameters, which is also a place where everyone is arguing.
Public class test2 {
?????? public static 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 changeta (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 is changed, then it is possible to say that the object is passed as a parameter, and the reference to the object is passed, and if the reference is changed within the method, the original object is also changed. From the output result of the above example, this explanation is reasonable.
Now we will change the above examples:
Public class test3 {
?????? public static 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 changeta (StringBuffer strbuf) {
?????? ??? strbuf = new stringbuffer ("hi");
?????????? Strbuf.Append ("world!");
???}
}
According to the experience in the above example: When the object is passed, the reference to the object is passed, and 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 running this program, you will find that the result is this:
Before change, SB = Hello
After 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 some have changed the value 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.
Look at Test2 programs first:
StringBuffer SB = New StringBuffer ("Hello");
After this is executed, an SB object will be generated in the stack of memory. Take you see Figure 1:
?
?
As shown in Figure 1, SB is a reference, which is stored in an address "@ 3A" (this "@ 3A" is an example of me a memory address. You just need to know whether it is a memory address, and this The address is exactly "hello" string of the address in memory.
Changedata (SB);
@ 3a
Hello
SB
figure 1
After implementing this sentence,
SB
Pass it
Changedata
In the method
StringBuffer strbuf
,due to
SB
The address is stored, so
Strbuf
Also store the same address, please see Figure 2:
@ 3a
Hello
SB
figure 1
@ 3a
Hello
SB
figure 1
@ 3a
Hello
SB
@ 3a
Strbuf
figure 2
at this time,
SB
with
Strbuf
Because the memory address stored is identical, it is pointed out.
Hello
".
Strbuf.Append ("world!");
After performing this sentence in the ChangeData method, change the value in the memory pointed to by StrBuf, as shown in Figure 3 below:
?
@ 3a
Hello World!
SB
@ 3a
Strbuf
image 3
So, Test2 This program will last output: after changeta (n), SB = Hello World!
?
Take a look at Test3.
@ 3a
Hello
SB
@ 3b
Strbuf
Figure 4
Hi
×
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 executing this sentence,
@ 3a
Hello
SB
@ 3b
Strbuf
Figure 5
Hi world!
As 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, the memory in the memory pointing to the SB does not change, so the following result is output. : After ChangeData (N), SB = Hello
?
The String class is a special class, which is overloaded, such as: string str = "hello"; equivalent to 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 String objects and basic in need to analyze it above. Like the type, in general, the value is transmitted as a parameter, and the value is changed within 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 run results are 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 changetata (n), SB =" SB); ???} ?????? ?????? public static void channelata (StringBuffer strbuf) {?????? ??? StringBuffer SB2 = New StringBuffer ("hi"); ??????????? StrBuf = SB2; ?????????? SB2.Append ("world!"); ???}}
?
??? Tip: ???????? After executing StrBuf = SB2;