Hello everyone
See this question today, three ways to summarize:
The first: This is one of the most common ways to develop, using the third variable, the readability is strong!
<strong><span style= "FONT-SIZE:18PX;" >public class Test{public static void Main (String [] args) {int m = 5, n = 2,max;if (n<m) {max = M;m = n; n = max; } System.out.println ("n=" +n+ "m=" +m); }}</span></strong>
The second way is to use two and make two number exchange! The third variable is not required, and the limitation is that n+m must be within the range of int
<strong><span style= "FONT-SIZE:18PX;" >public class Test{public static void Main (String [] args) { int n = 2, m= 5; n =n+m; m = n-m; n = n-m; System.out.println ("n=" +n+ "m=" +m); }}</span></strong>
third, do not use third-party variables, to achieve the size of the swap, skill way!
<strong><span style= "FONT-SIZE:18PX;" >public class Test{public static void Main (String [] args) { int n = 2, M = 5; n = n ^m; M = n^ m; (n^m) ^m n= n^m;<span style= "White-space:pre" ></span>//System.out.println ("n=" +n+ "m=" +m) ; }}</span></strong>
Results:
n=2 m=5
Two numeric comparison size