为什么没有更新该对象 [英] Why is the object not being updated

查看:21
本文介绍了为什么没有更新该对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这段代码,我想知道为什么引用的对象的值没有更改

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将指向对象引用的相同值。

这篇关于为什么没有更新该对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆