java向方法传递对象参数问题?为什么?

入坑小白,自学时遇到问题求大神解答!!!!!

public class Test1 {

public static void main(String[] args) {

Circle circle1 = new Circle(1);

Circle circle2 = new Circle(2);

swap1(circle1, circle2);

System.out.println("After swap1: circlel = " + circle1.radius + " circle2 = " + circle2.radius);

swap2(circle1, circle2);

System.out.println("After swap2: circlel = " + circle1.radius + " circle2 = " + circle2.radius);

}

public static void swap1(Circle x, Circle y) {

Circle temp = x;

x = y;

y = temp; }

public static void swap2(Circle x, Circle y){

double temp = x.radius;

x.radius = y.radius;

y.radius = temp;

}

}

class Circle {

double radius;

Circle(double newRadius) {

radius = newRadius;

}

}

这串代码我运行后结果为
图片说明

为什么swap1和swap2同样是传入引用类型参数,swap1方法体也创建了一个dui'x来进行交换,而swap1的内容输出后却没有进行交换?

求大神解答!!!!

回答

https://blog.csdn.net/caojidasabi/article/details/80080815

以上是 java向方法传递对象参数问题?为什么? 的全部内容, 来源链接: utcz.com/a/37456.html

回到顶部