Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the concept of handle in Java?

I read on a site ( http://www.rajeshpatkar.com/articles/javarefpnt/ ) that when we instantiate a class i.e

Emp e = new Emp();

A handle is stored in the variable e, which is not a pointer i.e it does not store the address of object in memory.

The explanation gives an example of array of pointers. The memory address is stored at a[1] position and when the object is moved this position is updated with new address.

So why this array is used instead of directly storing the address (it states that it helps GC, but i didn't understand it ) and updating it (the address stored in e) when the object is moved?

I've spent quite a lot of time in understanding this, but still haven't found an answer that satisfies me. So if you can explain me what actually is stored in the variable 'e' (with an example) it would be quiet helpful.

Thanks :)

like image 356
Jayesh Saita Avatar asked Oct 15 '25 03:10

Jayesh Saita


2 Answers

The usual term is "object reference" (or just "reference"), rather than "handle."

An object reference is an opaque value that uniquely identifies a certain object to the JVM. The form of that value is not defined by the specification other than. I suspect it's typically the size of an int or long, but I don't think even that is covered by either the JLS or the JVM spec. (To give you an idea, the JVM spec expressly points out that even the exact value of null [the special value meaning "no reference"] isn't mandated.)

References are not pointers, although of course since the form of a reference isn't specified, it would be possible for a JVM to be implemented by using pointers as references, so long as that fact couldn't be exploited in a way that violated the spec.

Because references are not pointers, Java doesn't have "pointer arithmetic" like C and its related languages do.

So if you can explain me what actually is stored in the variable 'e' (with an example) it would be quiet helpful.

It's not defined by the spec. It's just a value that uniquely identifies an object (and we can never see that value; the myth that the hex value you see when you use System.out to print an object that doesn't implement toString is the object's reference is just that: a myth). How that value identifies that object is up to the implementation of the JVM. It could be a pointer. It could be an index into an array of pointers. It could be more complex, using different bits from the reference value for different things.

like image 182
T.J. Crowder Avatar answered Oct 18 '25 04:10

T.J. Crowder


A handle is stored in the variable e, which is not a pointer i.e it does not store the address of object in memory.

To all practical purposes you can assume it stores the address of the object in memory.

However, consider in Java you cannot manage memory in an explicit way. That means you cannot make this kind of variables point to a particular position in memory. You can just make this variables point to a particular instance.

like image 40
Andres Avatar answered Oct 18 '25 02:10

Andres



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!