当运行函数foo1,为什么输出的代码将是:15 而不是15 5?别名中的对象
我明白了,对象v的指针现在指向对象va1,所以输出为代码行:System.out.print(v.getI()+“”); 应该是15.那为什么是30呢?
public class Value
{
private int _i;
public Value()
{
_i=15;
}
public int getI()
{
return _i;
}
public void setI (int i)
{
_i=i;
}
}
public class TestValue
{
public static void foo1()
{
int i=5;
Value v= new Value();
v.setI(10);
foo2(v,i);
System.out.print(v.getI() + " ");
System.out.print(i+ " ");
}
public static void foo2(Value v, int i)
{
v.setI(30);
i=10;
Value va1= new Value();
v=va1;
System.out.print (v.getI() + " ");
}
}
可能的重复[是Java“pass-by-reference”?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Radiodef