简体   繁体   English

对象和引用计数| Java的

[英]Objects and Reference Count | Java

Consider the following code snippet in Java: 考虑Java中的以下代码片段:

Customer obj1 = new  Customer();  
Customer obj2 = new Customer();  
Customer obj3 = obj2;  
obj3 = obj1;   

How many reference variables and objects are created here? 这里创建了多少个引用变量和对象? The solutions I came across were all confusing. 我遇到的解决方案都令人困惑。 Please explain. 请解释。

After

Obj3= Obj1;

You'll have two objects and 3 references. 你将有两个对象和3个引用。 Obj1 and Obj3 will reference to the same object. Obj1和Obj3将引用同一个对象。

Customer Obj1= new  Customer();  

// Customer object is created on the heap and obj1 refers it //在堆上创建Customer对象,obj1引用它

Customer Obj2= new Customer();  

//Customer object is created on the heap and obj2 refers it //在堆上创建Customer对象,obj2引用它

Customer Obj3= Obj2;  

// obj3 will refer to customer object created by obj2 // obj3将引用obj2创建的客户对象

 Obj3= Obj1;   

// obj3 (previosly refering to cust obj craeted by obj2 will be lost) and will now refer to cust obj created by obj1 // obj3(通常引用obj2引起的cust obj将丢失),现在将引用obj1创建的cust obj

Thus i would say 2 objects and 3 ref variables 因此,我会说2个对象和3个ref变量

  • obj1 and obj3 refering to Object created by obj1 obj1和obj3引用obj1创建的Object
  • obj2 refers to Object created by obj2 itself obj2指的是obj2本身创建的Object

Although the JLS doesn't forbid it, AFAIK no JVM uses reference counting, it is just too unreliable. 虽然JLS没有禁止它,但AFAIK没有JVM使用引用计数,它太不可靠了。 Note: C++ smart pointer uses references counts but these are very inefficient. 注意:C ++智能指针使用引用计数,但这些效率非常低。

You have up to three references to two different objects. 您最多有三个对两个不同对象的引用。

Note: unless your code does something useful with them the JVM can optimise this code away to nothing, in which case you will have no references or objects. 注意:除非您的代码对它们有用,否则JVM可以将此代码优化为零,在这种情况下,您将没有引用或对象。

假设custObj2new初始化,并从上面的片段初始化,其3个对象(包括custObj2 )和4个引用(包括Obj3

创建了三个变量和两个对象。

2 objects are created (the first 2 lines). 创建了2个对象(前2行)。

There are 3 reference variables created (Obj1, Obj2, and Obj3 are all reference variables.) 创建了3个引用变量(Obj1,Obj2和Obj3都是引用变量。)

The last 2 lines simply assign references to 2 different objects to Obj3. 最后两行简单地将对2个不同对象的引用分配给Obj3。

Step through it iteratively... 迭代地逐步...

Customer Obj1= new  Customer();  

One new object created, referenced by Obj1 创建了一个新对象,由Obj1引用

Customer Obj2= new Customer();  

A second object created, referenced by Obj2 创建的第二个对象,由Obj2引用

Customer Obj3= custObj2;  

Obj3, a reference variable, refers to custObj2 (which doesn't exist in this set of data, we'll assume it was created earlier?) Obj3,一个引用变量,引用custObj2 (在这组数据中不存在,我们假设它是先前创建的?)

Obj3= Obj1; 

Obj3 is re-assigned to point at Obj1. 重新分配Obj3指向Obj1。

In the end you have the three references, Obj1, Obj2, and Obj3 as well as 2 objects, (first two statements) and finally an ambiguous custObj2 ... if you meant to type Obj2 then ignore that part :) 最后你有三个引用,Obj1,Obj2和Obj3以及2个对象(前两个语句),最后是一个不明确的custObj2 ......如果你打算键入Obj2然后忽略那个部分:)

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

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