Java method parameter passing (Swap function) (reprint)

Source: Internet
Author: User

There is only one way to pass the parameters of a method in Java: Value passing. The value is passed by passing a copy of the actual parameter value (the replica) into the method, and the parameter itself is unaffected.

public class Primitivetransfertest

{

public static void swap (int a,int b)

{

int temp=a;

A=b;

B=temp;

System.out.println ("The value of a in the swap method 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 swap is over, the value of variable A is" +a+ "; the value of variable B is" +b ");

}

}

Run structure:

In the Swap method, the value of a is the value of 9;b 6;

After the swap is over, the value of variable A is 6; the value of variable B is 9;

As can be seen from the running results, the variables A and B inside the main method are not a and b inside the swap method. , that is, A and b of the swap method are just copies of the variables A and B inside the main method.

Java program starts from the main method, the main method begins to define a, b two local variables, when the program executes the swap method, the system enters the swap method, and the main method of a, b variables as the parameter value of the Swap method, the incoming swap method is only a A/b copy, Instead of A and b itself, the system generates 4 variables after entering the Swap method, A and B of the B,swap stack area of the main stack.

When you call the Swap method in the main method, the main method is not finished. Therefore, the system allocates two stacks for the main method and the Swap method, which holds the local variables of the main method and the Swap method respectively. The A and B variables in the main method are passed into the swap method as parameter values, in fact, the two variables A and b are re-generated in the Swap method stack area, and the values of A and B variables in the main method stack are assigned to the A and B parameters of the Swap method stack (that is, a, The B-parameters are initialized). At this point, the system has two a variables, two B variables, just exist in the different method stack area. Two outputs, one is a, b in the output swap method, one is a, b in the main method, the program only changes A and B in the Swap method, and a and B in the main method do not change. This is the essence of value passing: When the system starts to execute the method, the system initializes the parameter to the parameters of the method, which is not the actual argument variable, but the value of the argument variable.

The preceding is a basic type of parameter passing, and Java is passed as a value pass for arguments of a reference type.

Class Datawrap

{

public int A;

public int b;

}

public class Referencetransfertest

{

public static void Swap (Datawrap DW)

{

int TMP=DW.A;

dw.a=dw.b;

dw.b=tmp;

System.out.println ("Swap method, the value of the A attribute is" +dw.a+ "; the value of the B attribute 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 swap is over, the value of the A property is" +dw.a+ "; the value of the B property is" +dw.b ");

}

}

Operation Result:

In the Swap method, the value of the A property is the value of the 9;b property is 6

After the swap is over, the value of the A property is the value of the 9;b property is 6

From the results of the operation, the Swap method and the main method of a, b two attribute values are exchanged, it is easy to create an illusion: when the Swap method is called, the input method is the DW object itself, not its copy. But it's just an illusion.

The program starts with the main method, the main method starts creating a Datawrap object, and defines a DW reference variable to point to the Datawrap object, which is a different place from the base type. When an object is created, there are two things in the system memory: the object itself is saved in the heap memory, and the reference variable that references the object is saved in the stack memory. Next, the main method begins to call the Swap method, the main method does not end, the system will open up the main and swap two stacks, for the main and swap methods to hold local variables. When calling the Swap method, the DW variable is passed as an argument to the Swap method, and the same value is passed: The value of the DW variable in the main method is assigned to the DW parameter in the Swap method, thus the initialization of the unit parameter of the Swap method is completed. It is worth noting that the DW of the main method is a reference that holds the address value of the Datawrap object, and when the value of the DW is assigned to the DW parameter of the Swap method, the DW parameter of the Swap method also holds the address value, which is also referred to the Datawrap object in the heap memory. When the program operates the DW parameter in the Swap method, the Datawrap object in the heap memory is actually manipulated because the DW is just a reference variable. At this point, whether the DW object in the main method is manipulated or the DW parameter in the Swap method, the start is to manipulate the Datawrap object it refers to, and they manipulate the same object. Therefore, when swapping the A and B two properties of the Datawrap object referenced by the DW parameter in the Swap method, we can see that the value of a and B two properties of the Datawrap object referenced by the DW variable in the main method are also exchanged.

To better prove that the DW in the main method is two variables in the DW and swap methods, we add the following code to the last line of the Swap method:

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

Then, after the main method calls the Swap method, it accesses the A and B two properties of the DW variable again, and can still output 9, 6. After assigning a value of DW in the Swap method to null, the swap method loses the Datawrap reference and cannot access the Datawraper object in the heap memory. However, the DW variable in the main method is not affected and still references the Datawrap object, so you can still output the value of the A and B properties of the Datawrap object.

Java method parameter passing (Swap function) (reprint)

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.