ArticleDirectory
- Java parameter transfer (Classic)
Java parameter transfer (Classic)
16:46:29 | category: Java | Tag:SB changedata strbuf parameter passed hello |Font SizeSubscription
The parameter transfer mechanism in Java has been a constant debate. Whether it is "passing value" or "transferring address (transferring reference)", the two sides of the debate stick to each other and disagree with each other. Not only are "cainiao" confused, but some "old birds" only know the results but cannot tell why. I believe you will understand the following content.
First, let's look at the example of passing the basic type as a parameter:
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 ){
N = 10;
}
}
I think everyone in this example understands that when the basic type is passed as a parameter, it is a copy of the passed value. No matter how you change this copy, the original value will not change, the output proves this:
Before change, n = 3
After changedata (N), n = 3
Now let's take a look at the example of passing an object as a parameter, which is also a topic.
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 view the output result:
Before change, SB = Hello
After changedata (N), SB = Hello world!
From the result, the SB value is changed. Can it be said that when an object is passed as a parameter, the object reference is passed over. If the reference is changed in the method, the original object also changes. From the output results of the above example, this explanation is reasonable.
Now let's make a slight change to 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! ");
}
}
According to the experience in the above example: when an object is passed as a parameter, it passes the object reference. If the reference is changed in the method, the original object also changes. You will think the output should be:
Before change, SB = Hello
After changedata (N), SB = Hi world!
But run thisProgram, You will find the result is as follows:
Before change, SB = Hello
After changedata (N), SB = Hello
This is confusing. when an object is passed as a parameter, it also changes the value of the object in the method. Why does it change the value of the original object, but does some change the value of the original object? At this time, is it "pass value" or "transfer reference?
Next, let's take a closer look and explore the mysteries in the middle.
First look at the Test2 program:
Stringbuffer sb = new stringbuffer ("hello ");
After this statement is executed, an SB object is generated in the memory heap. See figure 1:
As shown in 1, Sb is a reference, which stores an address "@ 3A" (This "@ 3A" is an example of a memory address I mentioned, you only need to know that it is a memory address), and the address is the address of the string "hello" in the memory.
Changedata (SB );
After this sentence is executed, Sb is passed to stringbuffer strbuf in the changedata method. Because Sb stores the address, strbuf also stores the same address. See figure 2:
In this case, both SB and strbuf point to "hello" because the memory address is the same ".
Strbuf. append ("world! ");
After executing this sentence in the changedata method, the value in the memory pointed to by strbuf is changed, as shown in 3:
Therefore, the Test2 program will output:
After changedata (N), SB = Hello world!
Let's look at the test3 program.
Before strbuf = new stringbuffer ("hi"); is executed to the changedata method, the image in the memory of the object is the same as that in Figure 2 in the preceding example, after strbuf = new stringbuffer ("hi") is executed, it becomes:
In this case, what is stored in strbuf is no longer a "hello" address, but a "hi" Address "@ 3B" (also "@ 3B" is an example, after the new operator is successfully operated, a new storage area is always created in the memory.
Strbuf. append ("world! ");
After executing this sentence,
We can see that because the storage addresses in Sb AND strbuf are different, although the value in the memory to which strbuf points has changed, the value in the memory to which sb points does not change, therefore, the following results are output:
After changedata (N), SB = Hello
The string class is a special class, and some operators of it are overloaded, such:
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 only need to analyze according to the above method, and you will find that the string object is the same as the basic type. Generally, it is passed as a parameter and the value is changed in the method, the original object will not be changed.
In conclusion, we will understand that when an object in Java is passed as a parameter, the address of the object in the memory is copied to the parameter.
You can try to analyze the results of the examples in the example above to see if the running results are the same as those in 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:
After strbuf = sb2 is executed: