Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Object Reference Variable? [duplicate]

Tags:

java

What is Object Reference variable in java?

Does the reference variable holds the memory address of the object?

I am confused. Please do explain.

like image 476
user2301829 Avatar asked May 12 '13 04:05

user2301829


People also ask

What is an object reference variable?

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.

Can multiple object variables contain references to the same object?

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

What is meant by object reference?

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?

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.


3 Answers

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
like image 50
MadProgrammer Avatar answered Oct 16 '22 15:10

MadProgrammer


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.

like image 8
Stephen C Avatar answered Oct 16 '22 16:10

Stephen C


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.

like image 1
gifpif Avatar answered Oct 16 '22 17:10

gifpif