Passionate programmer
Passionate programmer

Reputation: 5898

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?

Upvotes: 4

Views: 2466

Answers (5)

OscarRyz
OscarRyz

Reputation: 199195

Does that mean it is pointer all the time?

Yes, but it can't be manipulated as you normally do in C.

Bear in mind that being Java a different programming language that relies on its VM, this concept ( pointer ) should be used only as an analogy to understand better the behavior of such artifacts.

Upvotes: -1

Umang Desai
Umang Desai

Reputation: 458

A primitive type is always passed by value. where as a Class Variable is actually a reference variable for the Object.

Consider a primitive type:

int i=0; 

now the value of this primitive type is stored in a memory location of address 2068. Every time you use this primitive type as a parameter, a new copy is created as it is not pass by reference but pass by value.

Now consider a class variable:

MyClass C1 = new MyClass();

Now this creates an object of the class type MyClass with a variable name C1.

The class variable C1 contains an address of the memory location of the object which is linked to the Valriable C1. So basically the class variable C1 points to the object location(new MyClass()).

And primitive types are stored in stack and objects in heaps.

Upvotes: 0

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147124

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.

Upvotes: 7

reccles
reccles

Reputation: 5224

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).

Upvotes: 3

Yishai
Yishai

Reputation: 91871

The answer is going to depend on every JVM implementation, but the best way to think of it is as a handle. It is a value that the JVM can look up in a table or some other such implementation the memory location of the reference. That way the JVM can move objects around in memory during garbage collection without changing the memory pointers everywhere.

Upvotes: 1

Related Questions