Number of objects and links | Java
Customer Obj1= new Customer();
// Customer object is created on the heap and obj1 refers to it
Customer Obj2= new Customer();
// The Customer object is created on the heap and obj2 refers to it
Customer Obj3= Obj2;
// obj3 will refer to the client object created by obj2
Obj3= Obj1;
// obj3 (the previous reference to cust obj craated by obj2 will be lost) and will now reference the cust obj created by obj1
So I would say 2 objects and 3 ref variables
- obj1 and obj3 referring to the object created by obj1
- obj2 refers to an object created by obj2 itself
source to share
Although the JLS doesn't prohibit it, AFAIK no JVM uses reference counting, it's too unreliable. Note. C ++ smart pointer uses reference count, but they are very inefficient.
You have up to three references to two different objects.
Note: if your code doesn't do something useful with them, the JVM can optimize that code to zero, in which case you won't have references or objects.
source to share
Go through iteratively ...
Customer Obj1= new Customer();
Created one new object referenced by Obj1
Customer Obj2= new Customer();
Created second object referenced by Obj2
Customer Obj3= custObj2;
Obj3, a reference variable, refers to custObj2
(which doesn't exist in this dataset, suppose it was created earlier?)
Obj3= Obj1;
Obj3 is reassigned to point Obj1.
In the end you have three references: Obj1, Obj2 and Obj3, as well as 2 objects (the first two operators) and finally ambiguous custObj2
... if you want to type Obj2 then ignore this part :)
source to share