int a = 1,b = 10;
int temp = a; ? //First assign the value of a to the intermediate variable temp.
a = b; //and then assign the value of b to a.
B = temperature; //Finally, assign the value of A stored in temp to B.
system . out . println(a+" "+b);
This exchange process is like a ring, there are three values on the ring, and the values are assigned to the front (back) variables clockwise or counterclockwise. So as to exchange data. Many sorting methods also put small values in front and large values in the back through intermediate variables.
The data exchange between two variables is realized by addition and subtraction operators, and no intermediate variables are needed.
int a = 10,b = 20
a = a+b; //At this time, a = 30
b = a-b; //b=30-20= 10, and a=30 remains unchanged.
a = a-b; //a=30- 10=20, and a changes.
system . out . println(a+" "+b);
Without using intermediate variables, the data exchange between two variables is realized by bit operators.
int a = 1 1,b = 14; //binary: a = 8-101,b =14-1/kloc-0.
a = a^b; ? //A b, XOR operation, only when the values of the corresponding digits are equal, can 1 be returned, otherwise, 0 can be returned.
//So a = ab =1011110 =10/0;
b = a^b; ? //b = ab =101110 =101,that is, the original value of b = a.
a = a^b; //A = AB =101011=10, that is, the original value of A = A.
system . out . println(a+" "+b);