Java exchanges the values of two variables.

Source: Internet
Author: User

Java exchanges the values of two variables.
1. parameter transfer method

To solve the title problem, we first introduce the parameter transfer method. Currently, there are three main parameter transfer methods for various programming languages:

The value-based transfer method (function) receives a copy of the variable provided by the caller without changing the parameter value) the variable address provided by the caller. the pointer passing method (function) receives a copy of the pointer provided by the caller without changing the value and address of the pointer, however, you can change the address pointed to by the pointer.

Ii. Java parameter passing Method

It is a pity that only one parameter passing method provided by Java is passed by value. That is to say, the method gets a copy of all the parameter values, and the content of the parameter variables passed to the method cannot be modified.

Java method parameter types can be divided into two types:

Anyone with Java development experience knows that the Java method cannot change variable content for basic data types. Can the content of the object reference of the custom class be modified? A simple example is provided. 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 {public static void change (MyClass myClass) {myClass. setNum (100);} public static void main (String [] args) {MyClass a = new MyClass (10); System. out. println ("the value before calling the change method is:" +. getNum (); change (a); System. out. println ("the value after calling the change method is:" +. getNum ());}}

The output result of the above code execution is as follows:

 

 

The results show that the change method can modify the object state. That is to say, the Java method can change the parameter status of an object. Does this mean that the Java method uses reference transmission for Parameters of Custom Data Types (custom classes? To confirm the results, you can write a simple example. All the custom classes are still the above MyClass. The Code is as follows:

Public static void swap (MyClass a, MyClass B) {MyClass tmp = a; a = B; B = tmp;} public static void main (String [] args) {MyClass a = new MyClass (10); MyClass B = new MyClass (100); System. out. println ("the value of a before the exchange is:" +. getNum (); System. out. println ("the value of B before the exchange is:" + B. getNum (); swap (a, B); System. out. println ("the value of a after the exchange is:" +. getNum (); System. out. println ("the value of B after exchange is:" + B. getNum ());}}

The execution result is as follows:

Through the above results, we can find that the Java method still uses value transfer, not reference transfer, to pass the parameters of the custom class. Why can the Java method modify the object status?

You can call the specific execution process of the change method to find the answer.

Call the change method. The specific execution process is as follows:

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

To sum up the usage of Java method parameters:

  • A parameter of the basic data type cannot be modified;
  • Methods can change the status of object parameters;
  • The method does not allow the object parameter to reference a new object (for the reason, refer to the specific execution process when the change method is called ).

3. Value of the exchange variable

Now that you know what it is, it is not difficult to know it. Paste my personal code directly:

 

public static void swap(MyClass a,MyClass b){        Object tmp=a.getNum();        a.setNum(b.getNum());        b.setNum(tmp);}

 

The execution result is as follows:

The exchange is valid.

References: Java core technology volume I.

 

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.