Topic
  • 3 replies
  • Latest Post - ‏2007-10-05T18:20:22Z by SystemAdmin
SystemAdmin
SystemAdmin
235 Posts

Pinned topic Object References

‏2007-03-20T20:27:33Z |
In C++ a pointer to an object points to the start of heap space where the object has been allocated.

In Java, where does an object reference point to:
- an actual memory location
  • a reference in a heap management process, which in turn points to the actual memory location

Thanks
Updated on 2007-10-05T18:20:22Z at 2007-10-05T18:20:22Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    235 Posts

    Re: Object References

    ‏2007-03-27T07:07:05Z  
    I don't know that the Java specifications actually mandate a particular implementation, but in practice references are handles that need to be dereferenced in order to obtain an actual memory address. You can see the effect of this in the JNI specification (http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html), where references that are passed to native code needed to be passed to special JNI methods in order to be used. Direct access to data, using a simple pointer, is only allowed for arrays of primitive types and even in that case requires you to first "pin" the array in memory so that the JVM garbage collection will not try to move the array while you're using it.
  • SystemAdmin
    SystemAdmin
    235 Posts

    Re: Object References

    ‏2007-09-03T09:18:23Z  
    I don't know that the Java specifications actually mandate a particular implementation, but in practice references are handles that need to be dereferenced in order to obtain an actual memory address. You can see the effect of this in the JNI specification (http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html), where references that are passed to native code needed to be passed to special JNI methods in order to be used. Direct access to data, using a simple pointer, is only allowed for arrays of primitive types and even in that case requires you to first "pin" the array in memory so that the JVM garbage collection will not try to move the array while you're using it.
    well a java reference variable holds just a value. This value is the means of reaching the object being refered by the variable. The object lies in memory. So immediate guess is that the object var holds the memory address of the object's residence in memory.
    Since java doesn't glorify the use of pointers so as usually expected, printing the object variable to console will give you a string which is something like:
    <class name>@<hexadecimal representation of the hashcode of class object>
    In Java we should be calling object reference variables as "references" and forgo the practice of using the word pointer.
  • SystemAdmin
    SystemAdmin
    235 Posts

    Re: Object References

    ‏2007-10-05T18:20:22Z  
    In C++ a pointer to an object points to the start of
    heap space where the object has been allocated.

    This is not always true, and certainly the spec doesn't say that. In particular, multiple inheritance is usually implemented such that some pointers have a value (address) that isn't at the start of the allocation. This is one way in which Java is simpler than C++.

    James