A numerical implementation method for Java Exchange two variables _java

Source: Internet
Author: User

A method of parameter transfer

To solve the title problem, the parameter transfer method is introduced first. At present, there are three kinds of parameter transfer methods in various programming languages:

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

Where the expression method (function) is passed by value receives a copy of the variable supplied by the caller, the value of the parameter is not changed; the variable address supplied by the caller that represents the method (function) is passed by reference; The pointer passing representation method (function) receives a copy of the pointer supplied by the caller, without changing the pointer's value and address. But you can change the address that the pointer points to.

Two, Java parameter Pass method

Java provides a method of parameter transfer, unfortunately only one, by value delivery. In other words, the method gets 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 grouped into two categories:

1. Basic data types
2. Object reference

Friends who have experience with Java development know that Java methods cannot change the content of variables for basic data types. Does the object reference to the custom class not modify the content? Can be illustrated by a simple example. The code is as follows:

PUBPC class myclass{
  private Object num;

  PUBPC MyClass (Object num) {
    this.num=num;
  }
  
  PUBPC Object Getnum () {return
    num;
  }

  PUBPC void Setnum (Object num) {
    this.num = num;
  }
}
Pubpc class Main {
  
  pubpc static void Change (MyClass MyClass) {
    myclass.setnum (MB);
  }
  
  
  
  Pubpc static void Main (string[] args) {
    MyClass a=new MyClass (a);
    System.out.println ("The value before calling the change method is:" +a.getnum ());
    Change (a);
    System.out.println ("The value after the call to 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 the object. This means that the Java method can change the parameter state of an object. Does that mean that the Java method uses a reference pass for the parameters of a custom data type (custom Class)? To confirm the results, you can write a simple example, and all the custom classes are still MyClass. The code is as follows:

Pubpc static void swap (MyClass A,myclass b) {
    MyClass tmp=a;
    a=b;
    b=tmp;
  }
  
  Pubpc static void Main (string[] args) {
    MyClass a=new MyClass (a);
    MyClass b=new MyClass (MB);
    System.out.println ("The value of a before the exchange is:" +a.getnum ());
    System.out.println ("The value of B before swapping is:" +b.getnum ());
    Swap (a,b);
    SYSTEM.OUT.PRINTLN (after "Exchange A's value is:" +a.getnum ());
    System.out.println (the value of B after swap is: "+b.getnum ());
  }
}

The results of the implementation are as follows:

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

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

To invoke the change method, the specific execution process is:

MyClass is initialized with the copy of Cheng Shi parameter A, which is a reference to an object.

The Setnum method applies to references to this object. The num of the MyClass object referenced by MyClass and A is changed to 100.

Parameter variable myclass is no longer used when the method is finished. And a continues to refer to the MyClass object that NUM becomes 100. As shown in the following figure.

So, the reason that Java method can change object parameter state is that the method gets the copy of object reference, the object reference and other copy in the method refer to the same object at the same time.

Now, summarize the use of Java method parameters:

Method cannot modify a parameter of a basic data type;

Method can change the state of object parameters;

Method cannot have an object parameter reference a new object (a reason reference to the specific execution procedure when the change method is called).

Iii. the value of the Exchange variable

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

Pubpc 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.

References: Java core technology Volume Ⅰ.

The above Java Exchange Two variables of the numerical implementation method is small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.