临时变量
通过临时变量实现交换:
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 5;
int temp;
temp = x;
x = y;
y = temp;
System.out.println("x = " + x + " y = " + y);
}
}
x = 5 y = 10
Process finished with exit code 0
两数相加
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
System.out.println("x = " + x + " y = " + y);
}
}
x = 5 y = 10
Process finished with exit code 0
运算符
public class Test {
public static void main(String[] args) {
int x = 10;
int y = 5;
x = x ^ y;
y = x ^ y;
x = x ^ y;
System.out.println("x = " + x + " y = " + y);
}
}
x = 5 y = 10
Process finished with exit code 0