当前位置: 首页 > 知识库问答 >
问题:

为什么对对象的第二次引用不会改变java中的内容?

缑赤岩
2023-03-14

在下面的代码中,我创建了电影类的3个实例。我已经为这三个对象分配了一些数据。其次,我创建了对对象movie1(movie4)的第二个引用。我还为电影4的属性赋值。我打印了电影1的新属性,但内容没有改变。为什么会发生这种情况?确切的原因是什么?我知道更新内容的其他方法,但我想知道为什么第二次引用不起作用
内容是movie1,但仍然相同。为什么?

class Driver{

    public static void main(String[] args){
        
        Movie movie1 = new Movie("The Shawshank Redemption", 1994, 9.3);
        Movie movie2 = new Movie("The Godfather", 1972, 9.2);
        Movie movie3 = new Movie("The Dark Knight", 2008, 9.0);

        **Movie movie4 = movie1;
        movie4 = new Movie("The Return of the King", 2003, 8.9);**

        movie4.name = "The Return of the King";
        movie4.year_of_release = 2003;
        movie4.rating = 8.9;
        System.out.println("\n*********New Movie 1 Details*************\n");
        movie1.printDetails();

    }
}

共有2个答案

叶允晨
2023-03-14

执行此命令后

Movie movie4 = movie1;

movie4的值是对movie1的引用,因此此时对movie4所做的更改将反映在movie1中。

movie4 = new Movie("The Return of the King", 2003, 8.9);

但是在这个命令之后,movie4不再包含对movie1的引用,它的值是一个全新的引用。因此,所做的任何更改仅在movie4指向的新对象上进行,而不是在movie1

步建茗
2023-03-14

这使得movi4指向movi1

Movie movie4 = movie1;

但是,随后您立即更新了movie4以指向不同的对象:

movie4 = new Movie("The Return of the King", 2003, 8.9);

因此,此时,movie1movie4不再指向相同的Movie,因此通过movie4进行的更改不会影响movie1指向的对象。

如果您在没有替换movi4的情况下更改了对象:

Movie movie4 = movie1;
movie4.name = "The Return of the King";

...那么无论您查看movie1.name还是movie4.name,您都会看到对象的更改。

更详细一点:

在你这么做之后:

Movie movie1 = new Movie("The Shawshank Redemption", 1994, 9.3);
Movie movie2 = new Movie("The Godfather", 1972, 9.2);
Movie movie3 = new Movie("The Dark Knight", 2008, 9.0);

...您的内存中有类似的内容(省略了很多细节):

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie1:Ref4516−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Shawshank..." |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie2:Ref8469−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Godfather"    |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie3:Ref4789−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Dark Knight"  |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

然后你这样做了:

Movie movie4 = movie1;

...制作movie4movie1指向同一电影:

                  
movie1:Ref4516−−−+
                 |    +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                 +−−−>|         (Movie)          |
                 |    +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie4:Ref4516−−−+    | name: "The Shawshank..." |
                      | ...                      |                               
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie2:Ref8469−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Godfather"    |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie3:Ref4789−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Dark Knight"  |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

到目前为止,还不错,但你做到了:

movie4 = new Movie("The Return of the King", /*...*/);

...这使得movie4指向一个新对象:

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie1:Ref4516−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Shawshank..." |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                                                 
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie2:Ref8469−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Godfather"    |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie3:Ref4789−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Dark Knight"  |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie4:Ref9546−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Return of..." |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

因此,分配给movie4.name只是更改了新对象,而不是movi1指向的对象。

如果您没有这样做,那么movie4=新电影(/*…*/)bit,那么movie1movie4仍然指向同一对象

                  
movie1:Ref4516−−−+
                 |    +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                 +−−−>|         (Movie)          |
                 |    +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie4:Ref4516−−−+    | name: "The Shawshank..." |
                      | ...                      |                               
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie2:Ref8469−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Godfather"    |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie3:Ref4789−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Dark Knight"  |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

...然后分配给movie4.name将更改movi4movi1指向的对象:

movie4.name = "The Return of the King";
                  
movie1:Ref4516−−−+
                 |    +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                 +−−−>|         (Movie)          |
                 |    +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie4:Ref4516−−−+    | name: "The Return of..." |
                      | ...                      |                               
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie2:Ref8469−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Godfather"    |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
movie3:Ref4789−−−−−−−>|         (Movie)          |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
                      | name: "The Dark Knight"  |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+
 类似资料:
  • 本文向大家介绍Java 中的 String对象为什么是不可变的,包括了Java 中的 String对象为什么是不可变的的使用技巧和注意事项,需要的朋友参考一下 什么是不可变对象? String对象是不可变的,但这仅意味着你无法通过调用它的公有方法来改变它的值。 众所周知, 在Java中, String类是不可变的。那么到底什么是不可变的对象呢? 可以这样认为:如果一个对象,在它创建完成之后,不能再

  • 问题内容: 我有一个班级和年龄段。但是,当我更改对象的年龄时,该类的所有其他对象也会更改。 有人可以帮助解决这个问题吗? 问题答案: 静态变量 是 类变量, 因此从变量声明中删除为: 根据java docs: 在声明中具有static修饰符的字段称为静态字段或类变量。它们与类关联,而不与任何对象关联。该类的每个实例共享一个类变量,该变量位于内存中的一个固定位置。

  • Java对象是在堆中创建的,为了在Java中进行垃圾收集,堆被分为三个部分或三代,它们被称为堆的年轻一代、终身或老一代和永久区域。新一代被进一步划分为三个部分,称为伊甸园空间、幸存者1和幸存者2空间。当第一次在堆中创建的对象在Eden space内的新一代中创建时,如果对象幸存,则在后续的次要垃圾收集之后,在主要垃圾收集将该对象移动到旧代或终身代之前,将其移动到幸存者1,然后移动到幸存者2。 阅读

  • 问题内容: 为什么Redux中的对象是不可变的?我知道某些框架(例如Angular2)将使用onPush并可以利用不变性来比较视图状态以更快地呈现,但是我想知道是否还有其他原因,因为Redux与框架无关,但它在其自己的文档中提到要使用不变性(与框架无关)。 感谢任何反馈。 问题答案: Redux是一个小型库,将状态表示为(不可变的)对象。和 新状态 通过将当前状态传递给纯函数来创建全新的对象/应用

  • 考虑类家长: 和类儿童: 如果我在应用程序中创建一个新的子对象,并将其放入父对象的children字段中,然后持久化父对象,为什么Hibernate不自动更新子对象的Parent字段(以便设置parentID列)?

  • 问题内容: 我正在向我的朋友解释OOP。我无法回答这个问题。(我有多可耻? 我只是想逃避,因为OOP描绘了现实世界。在现实世界中,父母可以容纳孩子,但孩子不能容纳父母。OOP也是如此。我知道它很愚蠢。:P 为什么此陈述无效? 因为aChild的成员是aParent成员的超集。那为什么孩子不能容纳父母。 问题答案: 正是因为aChild是aParent功能的超集。你可以写: 因为每只狐狸都是动物。但