Java Exchange values for two variables

Source: Internet
Author: User

first, the parameter passing method

To solve the title problem, first introduce the parameter passing method. At present, there are three kinds of parameter transfer methods in all kinds of programming languages:

    1. Pass by value
    2. Pass by reference
    3. Pass by pointer

Where the value-passing representation method (function) receives a copy of the variable supplied by the caller, does not change the value of the parameter, passes the variable address provided by the caller of the Representation method (function) received by reference, and passes the representation method by pointer (function) receives a copy of the pointer supplied by the caller without changing the value and address of the pointer. But you can change the address that the pointer points to.

Second, Java parameter passing method

Java provides a method of parameter passing, unfortunately there is only one, passed by value. That is, the method obtains a copy of all the parameter values, and the method cannot modify the contents of the parameter variables passed to it.

The Java method parameter types can be divided into two categories:

    1. Basic data types
    2. Object reference

A friend with experience in Java development knows that Java methods cannot change the contents of variables for basic data types. Does the object reference to the custom class also not modify the content? Can be illustrated by a simple example. The code is as follows:

 Public class myclass{    private  Object num;      Public MyClass (Object num) {        this. num=num;    }           Public Object Getnum () {        return  num;    }      Public void setnum (Object num) {        this. num = num;    }} 

 Public class Main {        publicstaticvoid change (MyClass MyClass) {        Myclass.setnum ( );    }                  Public Static void Main (string[] args) {        MyClass a=new MyClass (ten);        System.out.println ("The value before calling the change method is:" +a. Getnum ());        Change (a);        System.out.println ("The value after calling the change method is:" +a. Getnum ());}    }

The above code executes the output as follows:

As you can see from the results, the change method can modify the state of an object. This means that the Java method can change the object's parameter state. Does this mean that the Java method takes a reference pass for the parameters of the custom data type (custom Class)? In order to confirm the result can be written a simple example, all the custom classes are still the above MyClass. The code is as follows:

 Public Static voidSwap (MyClass A,myclass b) {MyClass tmp=A; A=b; b=tmp; }         Public Static voidMain (string[] args) {MyClass a=NewMyClass (10); MyClass b=NewMyClass (100); System.out.println ("The value of a before swapping is:" +a.getnum ()); System.out.println ("The value of B before swapping is:" +b.getnum ());        Swap (A, b); System.out.println ("The value of a after Exchange is:" +a.getnum ()); System.out.println ("The value of B after Exchange is:" +b.getnum ()); }}

The results of the implementation are as follows:

The above results show that the Java method is still using the value pass for the parameter passing of the custom class, not the reference pass. So why is the Java method able to modify the state of an object?

Consider calling the specific execution of the change method to find the answer.

Call the Change method thing, the specific execution process is:

    1. MyClass is initialized with copy of Narimi parameter A, which is a reference to the object.
    2. The Setnum method applies to a reference to this object. Num of the MyClass object referenced by MyClass and A is changed to 100.
    3. When the method ends, the parameter variable myclass is no longer used. And a continues to refer to the MyClass object that NUM becomes 100. As shown in.

Therefore, the reason that the Java method can change the state of an object parameter is that the method obtains a copy of the object reference, and the object reference and other copies of the method refer to the same object at the same time.

Now, summarize the Java method parameters using:

    • Method cannot modify the parameters of a basic data type;
    • Method can change the state of the object parameters;
    • The method cannot have the object argument refer to a new object (reason references the specific execution process when the change method is called).

Third, the value of the Exchange variable

Now that you know what it is, it's not difficult to know. Directly post my personal code:

 Public Static void Swap (MyClass A,myclass b) {        Object tmp=a.getnum ();        A.setnum (B.getnum ());        B.setnum (TMP);}

The results of the implementation are as follows:

Exchange is valid.

Reference: Java Core Technology Volume Ⅰ.

Java Exchange values for two variables

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.