为什么没有更新该对象
我编写了这段代码,我想知道为什么引用的对象的值没有更改
Java中的所有调用都是按值调用的。但当调用引用同一对象时,为什么不更新该对象
class Main {
public static void main(String[] args) {
Integer n = 3;
triple(n);
System.out.println("Hello world! " + n);
}
public static void triple(Integer x)
{
x *= 8;
System.out.println("Hello world! " + x);
}
}
实际产量
Hello world! 24
Hello world! 3
但我认为输出应该是
Hello world! 24
Hello world! 24
是否因为Integer类的取消装箱和自动装箱而创建了与‘x’同名的新对象,因为Integer是本地可用的不可变对象,并且不指向传递的参数/对象-n。
解决方案
将Integer n传递给triple方法时,实际上是在传递Integer对象n的引用的值。
因此,在triple方法中,另一个局部变量x在被调用时指向this引用值。在方法内部,当它将此Integer对象的值乘以8时,它将创建Integer的另一个新实例,因为Integer是不可变的,局部变量x将指向该实例。
因此您不会在print语句中看到这个新值,因为n仍然指向Integer的旧实例,该实例仍然是3。
如果您使用StringBuilder尝试此操作,您将获得预期的结果:
public static void main(String[] args) {
StringBuilder n = new StringBuilder("foo");
triple(n);
System.out.println("Hello world! " + n);
}
public static void triple(StringBuilder s) {
s.append("bar");
System.out.println("Hello world! " + s);
}
此处的输出将为:
Hello world! foobar
Hello world! foobar
这是因为StringBuilder是可变的,如果triple通过追加数据来修改数据,则原始指针n和新指针s将指向对象引用的相同值。
相关文章