Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is reference to java object is implemented?

Is pointer is just used for implementing java reference variable or how it is really implemented? Below are the lines from Java language specification

4.3.1 Objects An object is a class instance or an array. The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

Does that mean it is pointer all the time?

like image 766
Passionate programmer Avatar asked Mar 01 '23 04:03

Passionate programmer


2 Answers

In modern JVMs, references are implemented as an address.

Going back to the first version of HotSpot (and a bit earlier for the "classic VM"), references were implemented as handles. That is a fixed pointer to a pointer. The first pointer never changes for any particular object, but as the object data itself is moved the second pointer is changed. Obviously this impacts performance in use, but is easier to write a GC for.

In the latest builds of JDK7 there is support for "compressed oops". I believe BEA JRockit has had this for some time. Moving to 64 bit systems requires twice as much memory and hence bandwidth for addresses. "Compressed oops" takes advantage of the least significant three or four bits of address always being zero. 32 bits of data are shifted left three or four bits, allowing 32 or 64 GB of heap instead of 4 GB.

like image 83
Tom Hawtin - tackline Avatar answered Mar 02 '23 17:03

Tom Hawtin - tackline


You can actually go and get the source code from here: http://download.java.net/jdk6/source/

The short answer to your question is: yes, there is a pointer to a memory location for your java variables (and a little extra). However this is a gigantic oversimplification. There are many many many C++ objects involved in moving java variables around in the VM. If you want to get dirty take a look at the hotspot\src\share\vm\oops package.

In practice none of this matters to developing java though, as you have no direct way of working with it (and secondly you wouldn't want to, the JVM is optimized for various processor architectures).

like image 42
reccles Avatar answered Mar 02 '23 17:03

reccles