ArticlesJava's Object Reference Pointer or Handle ?


When we create a reference variable for an object in java , what are we suppose to think of it ?
Is it a structure or is it a simple pointer or a handle ?

The class information about a given object is within the reach of each object. This is because java has a meta-class called "Class". I will name is meta-class rather than "Class". This meta-class is used by JVM to create an object which stores information about a given class. For each class an object is created. All these meta-class objects represents the total information about all the classes at runtime. Say for instance

Class XYZ { ...} is loaded by the class loader . An object of meta-class is instantiated . This object stores all the information of class XYZ. So we have runtime information about each class. We call this object meta-object .

When objects of class XYZ are instantiated ( in heap) they have reference to this meta-object . Thus object know about their class through this meta-object. Don't ask how did you get the reference to the meta-class in class XYZ because you never had defined such a field. This field is obtained through inheritance from the Object Class.

The above three paragraphs are described to prove that the reference variable need not be a structure because it need not store the type information. This information is already there in every object.

Is Reference a Pointer?
This argument sounds good . Because what a reference needs to do is to store the address of the object. That's what references are all about references are pointers which point to a given object of it's type but can be treated as the name of the object.


References in C++
Let's take a case of C++ reference:


int i;
int & j = i ;
int *p = &i;
Here i is an integer variable and j is a reference . Actually j is a pointer and seperate memory is allocated for it but compiler treats it in a different way from p.

When we say p = p + 1 value of p is incremented. When we say j = j + 1 value of i is incremented , This means when we write j compiler converts it to * jp . Where jp is the pointer allocated for the concept of j . jp stores the address of i.

j = j + 1 ----> *jp = *jp + 1

When we say &p we get address of p

When we say &j we get address of i because

&j ---> &(*jp) ----> jp ( address of i)

So when the name of the reference is used actually the object is referred to. This poses a limitation that one cannot change the contents of j directly ( ie to say contents of jp cannot be changed)

int m ;
int & k = m;


what happens when we say

j = k

m is loaded in i.

References in Java
XYZ j , k;
j = new XYZ(); // object created and address stored in j say obj1
k = new XYZ(); // object created and address stored in k say obj2


what happens when we say j = k ;

C++ programmer may be prompted to say that object obj2 is copied in obj1,but here the reference of Java differ.

The address contained in k is copied in j . So both j and k start pointing to obj2 . As object obj1 losses reference it is latter on de-allocated by the garbage collector. So during assignment reference variable act as pointers but during normal usage act as the objects name. But the java's object reference is not a pointer because it does not store the address of the object why ? read ahead.

Is java Reference an Handle
Handle is one step above pointer . Pointer refer to the object in memory by storing its address. Let's take an example , suppose operation system is suppose to allocate a buffer when you make a get_buffer call .


char * p ;
p = get_buffer();


Now you program starts using p . Later on for some reason , say multitasking or otherwise the OS needs to change the position of the buffer in memory it cannot . Because you program is using the buffer's address. If the buffer's position is changed than the OS need to inform the pointer p about the change. I don't know how easy !! it will be to be managed if it can be at all. This can happen with several buffers.

What OS need to do is provide an array of pointer. Assuming that OS can at the most provide 10 buffers . char * a[10]; When a program ask the OS for buffer the OS allocates the buffer and stores the address of that buffer in the array say at position 1 and return these position to the program.

int i;
i = get_buffer();

Now if the buffer position has to be changed in memory, OS can do it without affecting the program . The buffer position can be changed and the new address can be stored in the position a[1] . The program refers to position 1 for buffer and gets the refreshed address . This is the handle approach the variable i in the above code is a handle and not a pointer.


Coming to the point

XYZ a1 ; // java object reference

Is "a1" a pointer or handle ? . It must be handle if the objects position in the memory ( heap) needs to be changed . If it is a pointer and the object's position is changed then the pointer will have wrong address. Is it that the object's position in the heap change after it is created ? Yes!!
When ? When garbage collector does the compaction , after de-allocating the unwanted objects . Thus java's reference is an handle to the table of pointers to java's objects.

back

support@rajeshpatkar.com
Rajesh Patkar Institute Of Software Engineering
26/A Patel Shopping Center, Ground Floor, Sainath Road, Opp Malad Subway, Malad (W), Mumbai.
Tel: 9820860292, 9820396074, 30903720, 28076817.