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