Passing Java method parameters (Java swap function)

Source: Internet
Author: User

In Java, there is only one way to pass the parameters of the method: value transfer. Passing a value is to pass a copy (replica) of the actual parameter value into the method, and the parameter itself will not be affected.

Public class primitivetransfertest

{

Public static void swap (int A, int B)

{

Int temp =;

A = B;

B = temp;

System. Out. println ("in the SWAp method, the value of a is" + A + "; the value of B is" + B );

}

Public static void main (string ARGs [])

{

Int A = 6;

Int B = 9;

Swap (A, B );

System. Out. println ("after the exchange ends, the value of variable A is" + A + "; the value of variable B is" + B );

}

}

Running structure:

In the swap method, the value of a is 9; the value of B is 6;

After the exchange ends, the value of variable A is 6, and the value of variable B is 9;

The running results show that the variables A and B in the main method are not A and B in the SWAp method ., That is to say, the and B of the SWAp method are only copies of the variables A and B in the main method.

Java programs start to run from the main method. The main method begins to define two local variables A and B. When the program executes the swap method, the system enters the swap method, the variables A and B in the main method are passed into the swap method as the parameter values. Only a/B Copies of the SWAp method are passed in, instead of A and B, after entering the swap method, the system generates four variables: A and B in the main stack, and A and B in the SWAp stack.

When the swap method is called in the main method, the main method is not finished yet. Therefore, the system allocates two stack zones for the main method and the swap method respectively to save the local variables of the main method and the swap method. Variables A and B in the main method are passed into the swap method as parameter values. In fact, two variables A and B are re-generated in the SWAp method stack, and assign the values of A and B variables in the main method stack area to the and B parameters in the SWAp method stack area respectively (that is, the and B parameters of the SWAp method are initialized ). At this point, the system has two A variables and two B variables, but they exist in different method stack zones. Two outputs: A and B in the SWAp method and A and B in the main method. The program only changes A and B in the SWAp method, the A and B in the main method have not changed. This is the essence of value transfer: when the system starts to execute the method, the system performs initialization for the form parameter, that is, to assign the value of the real parameter variable to the form parameter variable of the method, the operation in the method is not the actual real variable.

The previous section describes the transmission of parameters of the basic type. Java uses the value transfer method to transmit parameters of the reference type.

Class datawrap

{

Public int;

Public int B;

}

Public class referencetransfertest

{

Public static void swap (datawrap DW)

{

Int TMP = DW.;

DW. A = DW. B;

DW. B = TMP;

System. Out. println ("in the SWAp method, the value of attribute a is" + DW. A + "; the value of attribute B is" + DW. B );

}

Public static void main (string ARGs [])

{

Datawrap DW = new datawrap ();

DW. A = 6;

DW. B = 9;

Swap (DW );

System. Out. println ("after the exchange ends, the value of attribute a is" + DW. A + "; the value of attribute B is" + DW. B );

}

}

Running result:

In the swap method, the value of the attribute is 9; the value of the B attribute is 6.

After the exchange ends, the value of the attribute is 9; the value of the B attribute is 6.

According to the running results, the values of a and B of the SWAp method and main method are both exchanged, which can easily lead to the illusion that when the swap method is called, the passed swap method is the DW object itself, not its replica. But this is just an illusion.

The program runs from the main method. The main method creates a datawrap object and defines a DW reference variable to point to the datawrap object, which is different from the basic type. When an object is created, there are two items in the system memory: the object itself is saved in the heap memory, and the reference variables that reference the object are saved in the stack memory. Next, the swap method is called in the main method. The main method is not over. The system opens two stack zones, main and swap, respectively, to store the local variables of the main and swap methods. When the swap method is called, the DW variable is passed into the swap method as the real parameter, and the value passing method is also used: the DW variable value in the main method is assigned to the DW parameter in the SWAp method, to initialize the unit parameter of the SWAp method. It is worth noting that the DW of the main method is a reference, which saves the address value of the datawrap object. After the DW value is assigned to the DW parameter of the SWAp method, that is, let the DW parameter of the SWAp method also save this address value, that is, it will also reference the datawrap object in the heap memory. When the program operates the DW parameter in the SWAp method, since DW is only a reference variable, the actual operation is still the datawrap object in the heap memory. At this time, whether it is the DW object in the main method or the DW parameter in the SWAp method, the start operation is the datawrap object referenced by it. They operate on the same object. Therefore, when the swap method exchanges the attributes a and B of the datawrap object referenced by the DW parameter, we can see that the attributes a and B of the datawrap object referenced by the DW variable in the main method are also exchanged.

To better prove that DW in the main method and DW in the SWAp method are two variables, we add the following code in the last line of the SWAp method:

DW = NULL; // assign the DW value to null so that it no longer points to any valid address

Then, after the swap method is called by the main method, access the attributes a and B of the DW variable again, and the output is still 9 and 6. After the DW value in the SWAp method is null, the reference of datawrap is lost in the SWAp method and the datawraper object in the heap memory cannot be accessed. However, the DW variable in the main method is not affected and the datawrap object is still referenced. Therefore, the and B attribute values of the datawrap object can still be output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.