What is difference between the following in java :
Object
Reference ID
Reference Variable
When I see sta
Here:
Emp e=new Emp();
e
is reference variable which holds the address of an object which is created in heap area.
Reference id gets generated in form of a hash code, with help of magic method of object (object has total 9 methods) that is toString()
method; internally, using the toString
method of the object, it automatically generates Ref id for each object created at run time.
Memory for object always gets reserved in Heap area of RAM. bcz of there is no explicit pointer available in java. Here, ref variable points to the stack to give reference to object to reserve memory in Heap; bcz object also don't know where the memory is.
Stack segment is also part of memory: when we give ref variable then ref variable must be already stored in stack then ref variable knows which part of memory of head is free and it points to object reserve place and it hold in reference variable.
'new' is a textable operator which helps to create object.
Emp()
: is child class of class; if there no constructor provided in java program by programmer explicitly, then the compiler adds a default constructor implicitly, and reason to give the child class name as same class name is that so where "new" can easily knows how much memory is required for non static data members.
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.
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.
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..
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();
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'.