Old Articles‎ > ‎

Java's Object Reference Pointer or Handle

The Question
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 ? 
Is Reference a Pointer?
A pointer stores memory location of the target it wants to point. A pointer can be used to write or read from the target indirectly. However, a pointer never claims that it is like a target. Pointer is like a remote control pointing to a TV ( target ). With remote control you can control the TV but Remote control is not a TV. So if  we have a pointer  int *p  and a target  int i We can bind the pointer to the target p = &i. This means *p is equivalent to iHowever, the type of variable p is int *  and type of variable i is int 

A reference on the other hand poses itself as it's target. So if  we have a reference variable  int &p  and a target  int i We can bind the reference to the target p = i. This means p is equivalent to i. The type of variable p is int  and type of variable i is int. Internally, reference variable stores the address of the target, but at the developer's level of abstraction it poses itself to be of the target's type.


References in C++
Let's take a case of C++ references
Is Reference a Pointer?

int i; 
int & j = i ; 
int *p = &i;

Here i is an integer variable and j is a reference and p is a pointer . Actually j is a pointer and separate 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

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 target is referred. This poses a limitation that one cannot change the contents of j directly ( i.e to say contents of jp cannot be changed) 

Let's consider the following case. 


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 ( say obj1 ) and address stored in j 
k = new XYZ(); // object created ( say obj2 ) and address stored in k  


what happens when we say j = k ; ?

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 de-allocated by the garbage collector. During assignment reference variable act as pointers but during normal usage they act as the objects name. However, 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 a 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, but it can't. As your program is using the buffer's address, with the change in buffer's position the OS will need to inform the pointer p about the change. If buffer is relocated without considering the pointer, then we have a case of dangling pointer. This can happen with several buffers. 

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

int i; 
i = get_buffer(); 


If the buffer's position has to be changed in memory, OS can do it without affecting the program. The buffer's position can be changed and the new address can be stored in the position a[1] . The program refers to position 1 in the array for obtaining the the 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. 

In java 

XYZ a1 ; // java object reference 

Is "a1" a pointer or handle ? . It must be an handle if the object's position in the memory ( heap) needs to be changed . If it is a pointer and the object's position is changed,we get pointer with a wrong address. In Java, does the the object's position in the heap change after it is created ? Yes!!
 
When ? 
When garbage collector does the memory compaction, after de-allocating the unwanted objects. 

Thus, java's reference is an handle to the table of pointers to java's objects.