下面代码的输出是什么?( )
public class Test15 {
public static void main(String[] args) {
VO a = new VO(2);
VO b = new VO(3);
swapONE(a, b);
print(a, b);
swapTWO(a, b);
print(a, b);
}
private static void print(VO a, VO b) {
System.out.print(a.toString() + b.toString());
}
public static void swapONE(VO a, VO b) {
VO tmp = a;
a = b;
b = tmp;
}
public static void swapTWO(VO a, VO b) {
int tmp = a.x;
a.x = b.x;
b.x = tmp;
}
}
class VO {
public int x;
public VO(int x) {
this.x = x;
}
public String toString() {
return String.valueOf(x);
}
}2332
3232
3223
2323
对象作为参数时,参数变量是对象的引用,修改参数变量并不会修改对象本身,所以swapONE没有意义。
当修改参数对象的属性时,对象本身发生了变化,所以swapTWO数据会发生交换
当修改参数对象的属性时,对象本身发生了变化,所以swapTWO数据会发生交换