方式一:
int num1 = 10;
int num2 = 20;
//交换两个变量的值 方式一
int temp;
temp = num1;
num1 = num2;
num2 = temp;
System.out.println("num1 = " + num1 + ";num2 = " + num2);
方式二:
int num1 = 10;
int num2 = 20;
num1 = num2 + num1;//30
num2 = num1 - num2;//10
num1 = num1 - num2;//20
System.out.println("num1 = " + num1 + ";num2 = " + num2);
方式三:使用位运算符 公式:m = (m ^ n)^n;
int num1 = 10;
int num2 = 20;
int temp = num1 ^ num2;
num1 = temp ^ num1;
num2 = temp ^ num2;
System.out.println("num1 = " + num1 + ";num2 = " + num2);