What is Object Reference variable in java?
Does the reference variable holds the memory address of the object?
I am confused. Please do explain.
A reference variable is a variable that points to an object of a given class, letting you access the value of an object. An object is a compound data structure that holds values that you can manipulate. A reference variable does not store its own values.
Object references can be assigned between object reference variables. This means that the references in multiple reference variables can point to the same object (sharing).
The concept of object references becomes clear when assigning the same object to more than one property. Rather than holding a copy of the object, each assigned property holds object references that link to the same object, so that when the object changes all properties referring to the object reflect the change.
How many objects can be referenced from the same variables? Explanation: There should not be any confusion in how many references can be made from a single variable. A single variable can only point to one object at a time.
I'm not sure I have the elegance to properly answer this, but...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With