[Java] does not use third-party variables to exchange values of two variables. java third-party Variables

Source: Internet
Author: User

[Java] does not use third-party variables to exchange values of two variables. java third-party Variables

During language learning and program design, the most common way to exchange two variables is to use the temp new variable for conversion. The Code is as follows:

-----------------------------------------

<-- Standard algorithm -->

-----------------------------------------

Int a = 3, B = 4;

Int temp =;

A = B;

B = temp;

-----------------------------------------

This method is easy to understand and is especially suitable for beginners to understand the characteristics of computer programs. It is a classic Application of Value assignment statements. In actual development, this method is simple and clear, without ambiguity, and easy to communicate.

However, the disadvantage of this algorithm is that it is not very efficient to use a third-party variable-temporary variable.

-----------------------------------------

The following describes how to convert data without using temporary variables.

1. Arithmetic Algorithms

To put it simply, it is implemented by the common + and-operations. The Code is as follows:

-----------------------------------------

Int a = 3, B = 4;

A = a + B;

B = a-B;

A = a-B;

-----------------------------------------

The arithmetic principle is to regard a and B as points on the number axis and calculate the distance between two points.

Specific process:

In the first sentence, "a = a + B", calculate the sum of the distance between two AB points and save them in;

In the second sentence "B = a-B", find the distance from a to the origin and save it in B;

In the third sentence "a = a-B", find the distance from B to the origin and save it in.

Complete the exchange.

Compared with the standard algorithm, this algorithm has three more computing processes, but it does not use temporary variables.

2. XOR Algorithms

Variable exchange can also be achieved through exclusive or operations, which is perhaps the most amazing. Please refer to the following code:

-----------------------------------------

Int a = 3, B = 4;

A = a ^ B;

B = a ^ B;

A = a ^ B;

-----------------------------------------

The implementation of this algorithm is determined by the characteristics of the exclusive or operation. Through the exclusive or operation, some bits in the data can be flipped while others remain unchanged. This means that any number and any given value are two consecutive times without changing the value. That is:

A ^ B =. If a = a ^ B is substituted into B = a ^ B, B = a ^ B = a. Similarly, a = B ^ a = B; easy exchange.

The biggest benefit of exclusive or operations is that binary data operations are performed directly, greatly saving the conversion time efficiency.

-----------------------------------------

Nagging:

The tips in mathematics have a considerable influence on programming, and when used properly, they will have unexpected results.

 

Related Article

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.