Java parameter delivery

xiaoxiao2021-03-06  41

[Java diary] Java parameter passed the cold moon palace master Posted on 2004-12-22 0:34:01

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 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 when the object is passed, it is passed the reference to the object, if the reference is 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 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 passed, and if the reference is changed within the method, 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.

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

New Post(0)