简体   繁体   English

Java 对象引用和垃圾收集

[英]Java Object reference and garbage collection

I'm currently studying towards my Java associate certificate and I have the official textbook for study.我目前正在学习我的 Java 副证书,并且我有官方教科书可供学习。 In the text book is the following example code.在教科书中是以下示例代码。

public class Island {
    Island i;

    public static void main(String[] args) {

        Island i2 = new Island();
        Island i3 = new Island();
        Island i4 = new Island();

        i2.i = i3;
        i3.i = i4;
        i4.i = i2;

I understand here that I have initialised 3 Island objects with reference variables i2, i3 and i4 which each point to their own object.我在这里明白,我已经用引用变量 i2、i3 和 i4 初始化了 3 个 Island 对象,它们每个都指向自己的对象。 i2 is then redirected to point towards i3 and i3 towards i4 etc. What I don't understand is the need for the "i2.i" dot operator, what exactly is it doing here ?然后将 i2 重定向为指向 i3 和 i3 指向 i4 等。我不明白的是需要“i2.i”点运算符,它到底在做什么? is i2 = i3 not as equally valid ? i2 = i3 不是同样有效吗?

If anyone has any good resources on where I can read quite in depth into all of the applications of the dot operator in java that would also be helpful, thanks.如果有人在我可以深入阅读 Java 中点运算符的所有应用程序方面有任何好的资源,这也会有所帮助,谢谢。

You're using an instance variable of the same type as the class itself (see line Island i ).您正在使用与类本身相同类型的实例变量(请参阅Island i行)。 This means, that the class Island holds an attribute i of the same type Island .这意味着,类Island拥有相同类型Island的属性i Every island has therefore a link to another island.因此,每个岛屿都与另一个岛屿相连。 Your assignment of i2.i = i3;你对i2.i = i3;赋值i2.i = i3; defines the instance variable of i2 to be i3 .i2的实例变量定义为i3 In other words: i2 has a link to i3 .换句话说: i2有一个指向i3的链接。 You can get i3 if you have i2 .如果您有i2您可以获得i3

If you used the assignment i2 = i3 , the value of i2 would be overridden by the reference of i3 .如果您使用赋值i2 = i3 ,则i2的值将被i3的引用覆盖。 This means that i2 is not used anymore and the object behind i3 would also be the same object behind i2 (same object, 2 different variable names).这意味着不再使用i2并且i3后面的对象也将是i2后面的同一个对象(同一个对象,2 个不同的变量名称)。

It's important to be careful with the details here.重要的是要小心这里的细节。

You have indeed created three objects, but the objects are distinct from the variables .您确实创建了三个对象,但这些对象与变量不同

Let's simplfy: Consider Island x = new Island();让我们简化一下:考虑Island x = new Island(); , and Island y; , 和Island y; You have two variables , x and y , but only one object.您有两个变量xy ,但只有一个对象。 The object doesn't have a name, but it is bound to the variable x , so when you say x , you get that object.该对象没有名称,但它绑定到变量x ,所以当你说x ,你会得到那个对象。 And when you say y , you get nothing ( y is null).当你说y ,你什么也得不到( y为空)。

The dot accesses the object denoted by by the expression that precedes it.点访问由它前面的表达式表示的对象。 So xi accesses the i member variable of the object that is bound to x , and similarly, yi is attempting to access a member-variable of no object at all, which causes an exception to be thrown.所以xi访问绑定到x的对象的i成员变量,同样, yi试图访问一个根本没有对象的成员变量,这会导致抛出异常。

So now it is clear that you can say xi = x;所以现在很明显你可以说xi = x; to set the member variable Island.i of the object bound to x to the value that happens to be the same object.将绑定到x的对象的成员变量Island.i设置为恰好是同一对象的值。 Or you could set it to something else, like xi = new Island();或者您可以将其设置为其他内容,例如xi = new Island(); or xi = y;xi = y; . .

The dot doesn't have to be preceded by a variable, any expression will do.点前面不必有变量,任何表达式都可以。 For example, you could say (new Island()).i = x;例如,你可以说(new Island()).i = x; to create a new object (which again doesn't have a name; objects never have names) and bind the i member of that object to the object bound to x .创建一个新对象(它同样没有名称;对象永远没有名称)并将该对象的i成员绑定到绑定到x的对象。 Since this object is never bound to any variable, it is immediately eligible for collection.由于此对象从未绑定到任何变量,因此可以立即进行收集。

The point of your code example is that all objects are bound to variables that exist beyond the scope of the i1 , i2 and i3 (namely to the member variables of the three objects), and thus they form a reference cycle .您的代码示例的重点是所有对象都绑定到存在于i1i2i3范围之外的变量(即三个对象的成员变量),从而形成一个引用循环 An interesting question on the topic of garbage collection is whether the three objects are eligible for collection.关于垃圾收集主题的一个有趣问题是这三个对象是否符合收集条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM