2

Consider the following code snippet in 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.

5
  • 6
    What solution you came across? And what you are confused with? What is custObj2 there? Commented Jan 24, 2013 at 18:19
  • @millimoose Yeah, explaining it from that angle might make things more confusing for him/her though. Commented Jan 24, 2013 at 18:22
  • 3
    Here's a little puzzle. How many professional programmers does it take to write 2? The answer is below :) Commented Jan 24, 2013 at 18:24
  • 3
    @millimoose Of course there are references. It is even defined in the JLS: The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object. Commented Jan 24, 2013 at 18:24
  • @assylias Thanks for that, I guess I had the C++-ism on my mind. Commented Jan 24, 2013 at 19:02

7 Answers 7

3

After

Obj3= Obj1;

You'll have two objects and 3 references. Obj1 and Obj3 will reference to the same object.

Sign up to request clarification or add additional context in comments.

Comments

2
Customer Obj1= new  Customer();  

// Customer object is created on the heap and obj1 refers it

Customer Obj2= new Customer();  

//Customer object is created on the heap and obj2 refers it

Customer Obj3= Obj2;  

// obj3 will refer to customer object created by 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

Thus i would say 2 objects and 3 ref variables

  • obj1 and obj3 refering to Object created by obj1
  • obj2 refers to Object created by obj2 itself

Comments

1

Although the JLS doesn't forbid it, AFAIK no JVM uses reference counting, it is just too unreliable. Note: C++ smart pointer uses references counts but these are very inefficient.

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.

Comments

0

Assuming custObj2 is initialized with new, and from the above snippet, its 3 objects (including custObj2) and four references( including Obj3)

Comments

0

There are three variables and two objects created.

Comments

0

2 objects are created (the first 2 lines).

There are 3 reference variables created (Obj1, Obj2, and Obj3 are all reference variables.)

The last 2 lines simply assign references to 2 different objects to Obj3.

Comments

0

Step through it iteratively...

Customer Obj1= new  Customer();  

One new object created, referenced by Obj1

Customer Obj2= new Customer();  

A second object created, referenced by 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= Obj1; 

Obj3 is re-assigned to point at 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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.