9

What is Object Reference variable in java?

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

I am confused. Please do explain.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2301829
  • 231
  • 2
  • 4
  • 7

5 Answers5

21

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
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
8

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • this is nice and clean – Lisa Anne Oct 22 '16 at 13:37
  • So if reference variable's type is Object type or subclass of it, Can we say s1 is an object in "String s1= new String("string");" Further this code will create two objects s1 and object for "string" in memory ? – Dexter Dec 24 '17 at 15:15
  • 1) Strictly no. `s1` is a variable. `s1` *contains* an object reference, 2) No, because `s1` is not an object; see 1) – Stephen C Dec 24 '17 at 22:09
1

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.

gifpif
  • 4,507
  • 4
  • 31
  • 45
0

yes Object reference is the variable that holds the memory location of the real object

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
0

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

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424