gpt4 book ai didi

Java是按值传递吗?

转载 作者:行者123 更新时间:2023-11-29 06:09:20 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Is Java pass by reference?

为什么我的这段代码不起作用?我将一个对象传递给一个方法,但它不会修改我在 main 中创建的原始对象,这是为什么?

public class TestRun{

public static void main(String[] args){
Test t1 = new Test();
Test t2 = new Test();
t2.x = 555;
t2.y = 333;

System.out.println("The original valuess of X and Y are");
System.out.println("X = "+t1.x+"Y = "+t1.y);
modifyObject(t1,t2);
System.out.println("After the modification ");
System.out.println("X = "+t1.x+"Y = "+t1.y);


}

public static void modifyObject(Test arg1,Test arg2){
arg1 = arg2;
}
}

public class Test{

int x = 9999;
int y = 1;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com