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

why a second reference to an object does not alter the content in java?

在下面的代码中,我创建了 3 个电影实例 class。我已经为这三个对象分配了一些数据。其次,我创建了对对象 movie1(movie4) 的第二个引用。我还为 movie4 的属性赋值。 我打印了电影 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();

    }
}

这使得 movie4 指向 movie1:

Movie movie4 = movie1;

但是,然后你立即更新movie4指向一个不同的对象:

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

所以在那个时候,movie1movie4 不再指向同一个 Movie,所以你通过 movie4 所做的更改不会影响对象movie1 指向。

如果您 更改了 对象而没有替换 movie4:

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 只是更改新对象,而不是 movie1 指向的对象。

如果你没有做那个movie4 = new Movie(/*...*/)位,那么movie1movie4仍然指向同一个对象

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

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

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

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

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"  |
                      | ...                      |
                      +−−−−−−−−−−−−−−−−−−−−−−−−−−+

这条命令执行后

Movie movie4 = movie1;

movie4的值是对movie1的引用,所以在movie4上所做的修改此时会反映在movie1上。

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

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

上进行