All about ‘OBJECT REFERENCES’ in Java Programming Language !

In this article, I have given few ideas about object references in Java Programming Language in the from of question and answers. Enjoy the reading !

 

Question: What does an object reference actually contain ?

Answer: The object reference contains some bit patterns (like an address or a pointer) that which only JVM knows, using which, the JVM references ( or in more simple language, the JVM reaches out) to the actual object that resides on the heap. Please note, that, the object reference itself does not hold the object. It only references.

Example: 

Boy b = new Boy( );

In the above statement, ‘b‘ is just a reference to the ‘Boy‘ object thats created on the heap.

 

Question: What is the size of a reference variable ?

Answer: There is no precise or universally accepted size for an object reference. If you know anyone who works on the development side of the JVM, then, he/she would be the right person to guide you on how exactly the JVM is represented. But, if we wish to give a guess-try, then, the size of the reference variable could be guessed to somewhere either 32-bit or 64-bit based on the architecture on which your JVM is installed. Also it is very important to note that, the size of object references for a given JVM would be the same. Not necessary, the size of object reference on one JVM would be same on some other JVM. Differences would exist.

 

Question: How do we manage memory issues if we are not sure with the size of Object References ?

Answer: Your memory issues are something that has to do with the objects on the heap. Not on object references. Manage your objects well to manage your memory issues.

 

Question: How to do pointer arithmetic on ‘Object References’ ?

Answer: ‘Object Reference’ is not a pointer. It just looks like a pointer that points to an object on the heap. Do not take your comparison far beyond this. You cannot perform arithmetic operations on ‘Object References’.

 

Question: Is there a memory leak issue with the below statements ?

Boy b1 = new Boy( ); // ‘b1’ is an object reference
Boy b2 = new Boy( ); // ‘b2’ is an object reference

b1 = b2;

Answer: In the above statements, we can see that, ‘b1‘ is being replaced with ‘b2‘, therefore, losing its bit representation that pointed to the first object. This does not lead to memory leak, as our JVM cleans up the mess with the help of Garbage collector allowing no room for unreferenced objects.

 

 

About the Author:

Subhash.K.U, Principal Mentor,

Subhash Programming Classes, Bangalore

View My Complete Profile !

I love to teach you ! Click here !

My name is Subhash.K.U. I work as Principal Mentor at Subhash Programming Classes helping fresh engineering graduates and professionals crack their programming interviews through C, C++, Data Structures, Java and Linux Programming. 

No Comment

You can post first response comment.

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.