Ratan Kumar Ratan Kumar - 4 months ago 8
Java Question

What is the difference between an Object , Reference ID , and Reference Variable in Java?

What is difference between the following in java :


  1. Object

  2. Reference ID

  3. Reference Variable



When I see statements like this:

Emp e = new Emp();


Here
Emp
is class, but
e
is not its object? If that is so .. somewhere else I have seen this:

cos if it is so then ..somewhere is see like

Emp e = new Local();


Where local is a child class of
Emp
. So what does
e
mean in this case? What does it hold?

Answer

An object is, essentially, a chunk of memory living in the heap. (Part of the memory structure of objects includes a reference to the class of that object.)

Object variables in Java (like e, in this example) contain references to objects living in the heap.

Classes are completely different from all of these; they might describe the structure of objects of that type, and have method implementations and the like, but classes live in an entirely different area of memory from other objects.

Comments