public void swap(int x, int y)
{
int temp = x;
x = y;
y = x;
}
public void swap(Integer x, Integer y)
{
Integer temp = x;
x = y;
y = x;
}
异或
y = x ^ y;
x = x ^ y;
y = x ^ y;
加法:
x = x + y;
y = x - y;
x = x - y;
运算:(利用Java的变量缓存机制)
x = y + 0 * (y = x);
public class Main1 {
public static void main(String args[]) {
int a, b;
a = 5; b = 8;
a = b * 3 + (b = a) * 2 + (b = 7);
// System.out.printf("%d,%d",a, a + (0 * (a = b)) );注意Java的Printf也与C++参数入栈方式不同,
C++一般函数压栈是从右到左,构造函数初始化列表从左到右初始化;
}
}
0: iconst_5 常量5压入栈顶 5
1: istore_1 弹出常量5,赋值给a,a压入栈顶 a
2: bipush 8 常量8压入栈顶 8,a
4: istore_2 弹出常量8赋值给b,b压入栈顶 b,a
5: iload_2 b推到栈顶 b,a
6: iconst_3 常量3压入栈顶 3,b,a
7: imul 相乘3*b,结果temp1压入栈顶 temp1,a
8: iload_1 a推到栈顶 a,temp1
9: dup 弹出a,复制a值A压到栈顶 A,temp1
10: istore_2 弹出A,赋值给b,b压到栈顶 b,temp1
11: iconst_2 常量2压入栈顶 2,b,temp1
12: imul 弹出2,b;2 * b,结果temp2压入栈顶 temp2,temp1
13: iadd 双操作数,弹出temp1和temp2, temp2+temp1
计算temp1+temp2压入栈顶
14: bipush 7 常量7压入栈顶 7,temp2+temp1
16: dup 弹出7,复制常量7,压入栈顶 7,temp2+temp1
17: istore_2 弹出7,赋值给b,b压入栈顶 b,temp2+temp1
18: iadd temp1+temp2+b,结果result压入栈顶 result= b + temp2 + temp1
19: istore_1 result弹出,赋值给a a
20: return return