Saturday, April 19, 2014

The new operator in java


The new operator in java is responsible for the creation of new object or we can say instance of a class.
Actually, it dynamically allocates memory in the heap with the reference we define pointed from the stack.
The dynamically allocation is just means that the memory is allocated at the run time of the program. The advantage of this approach is that your program can create as many or as few objects as it needs during the execution of your program.

For example :
Before creating a new object of the class we create the reference variable of the class as

Demo obj;
(Where Demo is the class whose object we want to create)

Now this statement only creates a reference variable of a class or the "Demo" class data type.
There are data types such as integers, characters etc. but we dont need to create these with the help of
new operator because they are not implemented as objects in java rather they are implemented as normal variables because of the efficiency. Since all objects have some properties and behavior and require java to treat them differently than it treats the simple types.
Object version of these data types is also present with the help of wrapper classes which concept is not needed yet.
All the reference variables lies in the stack of the memory and hence obj is .
Currently the reference "obj" is pointed to the null or it is not pointing to any object.

If we use the new operator than the actual or physical creation of the object is occurred.
The statement is :
obj = new Demo();

This statement does two things :
1. It creates the actual or physical object in the heap with the variable obj pointing to the object from the stack.
2. It calls the constructor of the "Demo" class for the initialization of the object.


Basically, the "Demo()" in the statement is the default constructor.
A constructor defines what occurs when an object of the class is created.
Most real-world classes explicitly defines their own constructors within their class definition and if no explicit constructor is specified in the class then java will automatically supply a default constructor.

Also the two statements can be combined into one statement as :
Demo obj = new Demo();

Now at this time the obj which has points to the actual object of the Demo class can call any not restricted methods.
Consider the case, when we declare another reference of the class and equating that variable with the previous reference i.e, obj
Demo obj1;
obj1 = obj;

It is to be noted that the above statement cannot create new object in the memory but the obj1 reference variable now points to the same object which is pointed by the obj



Also at any time if we point obj object to null than it does not mean than the obj1 object is destroyed.

obj = null;

This means that the obj1 object will points to the same object but the obj will not points to any object.


So, this is all about the new operator.

0 comments to “The new operator in java”

Post a Comment

 

Information Sharing Copyright © 2016 -- Powered by Blogger