怎么会我的这个代码不工作?我将一个对象传递给一个方法,但它不会修改我在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;
}
C++中的非基元类型是一个对象,但在Java中它是隐含的引用。引用是按值传递的。 –