Java two variable exchange values

Source: Internet
Author: User

PackageTestPublicClassTest {
PublicStaticvoidMain (string[] args) {
IntA, B;
A = 10;
b = 12;
System.out.println (A + "" +b);

/*
* With third-party variables this algorithm is easy to understand, especially suitable to help beginners understand the characteristics of computer programs, is the classic application of assignment statements.
* In the actual software development, this algorithm is simple and clear, does not produce ambiguity, facilitates the communication between programmers. In general, we should adopt this algorithm (standard algorithm) when we encounter the Exchange variable value problem.
*/
int temp =A
A =b
b =Temp
System.out.println (A + "" +b);

/*
* Arithmetic algorithm Its principle is: The A, b as a point on the axis, around the distance between two points to calculate. Specific process:
* The first sentence "a=b-a" to find the distance ab two points, and save it in A;
* The second sentence "b=b-a" to find the distance from a to Origin (b to the origin of the distance from the AB two point distance), and save it in B;
* The third sentence "A=b+a" to find the distance from B to Origin (A to Origin distance and ab two point distance), and save it in a. Complete the interchange.
* This algorithm has more than three computational processes compared to the standard algorithm, but does not use temporary variables. (Arithmetic algorithm)
*/
A = B-A
B = B-A
A = B +A
System.out.println (A + "" +b);

/*
* XOR the same as 1 different for 0 This algorithm can be implemented by the characteristics of the XOR operation is determined by the XOR operation can make some bits in the data flip, the other bits unchanged.
* This means that any number is continuously different or two times with any given value, and the value does not change.
* That is: a^b^b=a. The a=a^b into the b=a^b is b=a^b^b=a, the same can be achieved a=b^a^a=b; Easy to complete the exchange.
*/
A = a ^ b; // 1010 ^ 1100 1001
b = a ^ b; // 1001 ^ 1100 1010
A = a ^ b; // 1001 ^ 1010 1100
System.out.println (A + "" + B);
}
}

Java two variable exchange values

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.