老样子,先举例:
public static void main(String[] args) {
Test t1 = new Test("张");
Test t2 = new Test("李");
int a = 1;
int b = 2;
swap(a,b);
swap(t1,t2);
System.out.println("t1:"+t1.name+" t2:"+t2.name);
System.out.println(a+" "+b);
}
public static void swap(int a , int b){
int c = a;
a=b;
b=c;
}
public static void swap(Test t1s , Test t2s){
Test t = t1s;
t1s=t2s;
t2s=t;
}
swap( )执行完毕,方法执行完毕出栈,然后所有东西销毁,也就是说只是swap中的值做了交换,对main方法a,b的值没有影响。
紧接着向下执行代码,swap()再次入栈并传入t1,t2地址
接着交换地址
方法完成, 方法出栈
可知t1和t2并没有做出任何改变