This question already has answers here:
How is reference to java object is implemented?
(5 answers)
Closed 9 years ago.
Since java does not support pointer(memory address) , then how is the reference exists in memory and how a reference variable use it ??
Since java does not support pointer
Yes it does. Why do you think there is a NullPointerException? Pointers can either be null or contain a reference to an object.
(memory address),
Exactly. It doesn't support memory addresses, and it doesn't support C/C++ semantics on pointers as memory addresses. All you can do with a Java pointer is assign it or dereference it.
then how is the reference exists in memory and how a reference variable use it ??
See the JLS: "The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object."
Related
I am coming to Java from C++ and am having trouble grasping the true distinction between an object and a reference to the object.
When an object is instantiated a region of memory is reserved and to access/operate upon the data in the memory we use the reference to the object. This i understand but in my textbook the author states,
"An object reference variable that appears to hold an object actually contains a reference
to that object. Strictly speaking, an object reference variable and an object are different,
but most of the time the distinction can be ignored."
So I am curious as to when this distinction cannot be ignored. It kind of sounds like the way Pointers are distinguished from the address they point to. Do all Java reference variables operate in a similar fashion as C/C++ pointer variables? I.E are reference variables simply a pointer or reference to a memory address?
I have already gone through What are classes, references and objects? and I am still unclear as to how the distinction actually matters when working in Java. If anyone could provide an explanation or a link to a thorough explanation It would be greatly appreciated.
This question already has answers here:
What is the difference between a pointer and a reference variable in Java?
(9 answers)
Closed 5 years ago.
Creating a reference in Java is same as the concept of pointer is exactly same, then why it is said that Java does not support pointers?
In C you can manipulate pointers to get somewhere else than the pointer points to. In Java references are atomic and only makes sense to get to an object in memory.
While searching for an explanation on how a reference variable is implemented in Java I came across this question:
What's inside a reference variable in Java?
In there was a comment by Samuel_xL saying that specifying the vendor name would be a better question.
So my question is that how an instance variable in implemented in Oracle JVM? Is it a pointer to an address? I know that a reference holds bits that tell the JVM how to access the object.
But how is it structured??
From what I've been able to determine, object references are stored either as a type called oop (ordinary object pointer) or narrowOop, depending on whether the JVM is using compressed object pointers or not. An oop is a C++ class that wraps a pointer to a Java object, and a narrowOop is a 32-bit unsigned integer that has to be converted into a proper pointer in order to access the object; they have no internal structure. You can find the declarations here: http://hg.openjdk.java.net/jdk7/jdk7/hotspot/file/tip/src/share/vm/oops/oopsHierarchy.hpp
This question already has answers here:
What exactly is a reference in Java?
(4 answers)
Closed 9 years ago.
What is Object Reference variable in java?
Does the reference variable holds the memory address of the object?
I am confused. Please do explain.
I'm not sure I have the elegance to properly answer this, but...
An Object is an instance of a Class, it is stored some where in memory
A reference is what is used to describe the pointer to the memory location where the Object resides.
A variable is a means by which you can access that memory location within your application (its value is "variable"). While a variable can only point to a single memory address (if its not null), it may change and point to different locations through out the life cycle of the application
What is Object Reference variable in java?
Simply, it is a variable whose type is an object type; i.e. some type that is either java.lang.Object or a subtype of java.lang.Object.
Does the reference variable hold the memory address of the object?
Probably yes, but possibly no.
It depends on how the JVM represents object references. In most JVMs, the object reference is represented behind the scenes using a memory address or pointer. But it could also be represented as an index into an array ... or something else. (Indeed, I've messed around with an experimental JVM where an object reference was actually an index into an array of pointers.)
The point is that Java object references are an abstraction that is designed to hide the representation / implementation details from you. The actual representation should not concern you ... since it doesn't matter if you program in pure Java. You can't get hold of the actual memory address in pure Java ... and that's a good thing. The JVM (specifically the garbage collector) is liable to change an object's actual memory address without telling you. If an application could obtain and use object addresses, it would need to deal with that, and it is a fundamentally difficult problem.
Object Reference variable is just like pointer in c but not exactly a pointer.
Its depend's upon JRE provide some JRE treated just like a pointer and some other JRE treated as pointer to pointer.
so refernce variable just define a way to reach your object. Java is platform independent language so memory management is different in different devices so its difficult to give a unique way to reach the object.
yes Object reference is the variable that holds the memory location of the real object
In Java all objects are referred to by references for instance
Object o = "foo";
The above example has a reference, o, to the object "foo".
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Creating a dangling pointer using Java
how to create dangling pointers in java but this time using arrays as memory allocators?
There can't be dangling pointers in Java. The language is defined in a way that makes it impossible.
Object will only ever be removed by garbage collection when they are no longer reachable.
The closest you can get to a dangling pointer is a reference that holds null (i.e. doesn't point to any object). But that's still a defined value with defined behaviour.
Since Java uses a garbage collector, it's impossible to create a dangling pointer in Java. I guess if you misinterpret what a "dangling pointer" is, you could see the null pointers a newly created array (of a reference type) is filled with as "dangling pointers".
You cannot create a dangling pointer or reference in Java Creating a dangling pointer using Java