What is difference between the following in java :
Object
Reference ID
Reference Variable
When I see sta
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();