saim saim - 1 year ago 113
Java Question

How did `new`operator initializes the memory using contructor?

RealEstate v=new RealEstate();

I have used this
keyword with

,i know
allocates memory and initializes the memory using constructor
.But my question is that how did this
operator initializes the memory using contructor?

rD. rD.
Answer Source

new operator doesn't actually uses the help from constructor to allocate memory. It have nothing to do with constructor. Basically Java's version of malloc is new.

new operator:
- invokes object constructor
- allocates memory and returns reference to that memory

Constructor is executed separately to perform any operations during initialization, like allocating values to objects and variables. If no Constructor is defined, then JVM will create default constructor and will allocate default values:

Data Type       Default Value (for fields)
double          0.0d
char            'u0000'
String          null
boolean         false
Any Object      null

new only allocates memory and returns reference to that memory.

see the documentation.

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.