简体   繁体   中英

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?

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.

Emp e

This statement creates a reference variable 'e' in the stack.

    new Emp()

This statement creates an object in the heap. An object is just a buffer or we can say "a chunk of memory". Hence , a buffer gets reserved in the heap. Thus the statement,

   Emp e=new Emp() 

passes the reference id of that object created in the heap to the reference variable 'e'.

Emp{
int salary;
int name;
}

New new Emp(); Emp is a class, new is used to reserve memory area in heap.New returns the address of memory it reserved. As soon as you write ,new Emp(),a memory buffer gets reserved in heap.Size of this reserved area depends upon size of data members of a class(Here it is 2 bytes since int salary takes 1 byte and int name takes 1 byte.)

Reference Id Reference id of an object is the address of location where object is stored. new Emp(); creates the object but its address is not stored and catched anywhere.

Reference Variable Now,Suppose you want to use the same object again and again,then you must have its address location(reference Id). For storing its addressid(ReferenceId) you can use reference variable. Reference variable always take 4 bytes and they are just variable which stores the address(reference of object).

Creation of reference variable Emp e2;

Assigning reference Id to a reference variable Emp e2=new Emp();

It's a simple question...

emp e=new emp();

Here, e is reference id to the object. emp is the reference variable to the class and your object id different its the combination of state and behaviour..

I just made a program for displaying reference ID of an object.

class abc
{

   int a=10;
   int b;
}

class t extends abc
{

   public static void main(String args[])
   {
     abc A=new abc();
     System.out.println(""+A);
   }
}

output : shockingly a hex string :

"abc@52e922"

Java maps the actual location of an object at a separate place in form of a hex string which is known as reference ID. It never displays actual location of it's object stored in the memory.

Car c=new Car();

Object is nothing it is just a buffer or memory in heap where non static data members gets the memory.

Reference ID is generated by new operator in the stack and it is a memory location which contains the memory location of an object in hashcode form. Reference ID is the only way to reach the object. Reference ID is generated because there is a rule in java that memory allocated at run time does not have any name and we all know that objects are created at run time so they also have no name and we need a unique ID to perform operation on that object that's why there is a unique Reference ID in java for each object.

In above example c is a reference variable which store the reference ID.

Object is nothing it is just a memory area or buffer in a heap area where all the instance data members of a class are getting a memory.

Emp e = new Emp();

in the above statement e is a reference variable which hold the reference id of the object, however in for security purpose java is not allowing anyone to get the id of actual object it also can be self explanatory as we are using word reference id which redirects us that we are not getting the actual id of our object instead just a reference of it.

also reference id would be nomenclature as classname@hexadecimal representation of the # code of object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM