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 don't 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'
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.
Awesome :D, nice post.
ReplyDeleteAn absolute explanation
ReplyDeleteDear Mr. Aqil, it is very encouraging to see your blog in Pakistan. In fact, I found the blog on java programming after searching for your name from a steel blog called "Build Industrial Pakistan" which I found very encouraging.
DeleteAs per the befitting title of your blog, I wish to extract metal from ores in private sector of Pakistan and will really appreciate your advice. I will be happy to share my phone number with you if you allow. Hope that you will be able to spare time to talk to a like minded person on cellphone soon!
Thanking you in advance,
Hassan (0333-4612611)
very good explanation ,simple english...and nice work with figure....awsome job man..
ReplyDeletethanks. :)
Deletein above code..i tried to implement your explanation but check after assigning null to obj then assigning to another ,all values r null.
ReplyDeleteclass XYZ{
Deleteint i;
void dis(){
System.out.println("neha" );
}
}
public class Demo3 {
public static void main(String[] args) {
XYZ obj=new XYZ();
XYZ o;
o=obj;
System.out.println("before assigning null:" + o);
obj=null;
o=obj;
System.out.println("After assigning null:" + o);
System.out.println(" What is inside dere: "+obj);
//obj.dis();
}
}
output
before assigning null:com.demo.XYZ@427b7b5d
After assigning null:null
What is inside dere: null
This comment has been removed by the author.
DeleteWhat he means to say is even when u say obj=null , the o variable still points to the xyz object....
DeleteBut here in your case after u have made obj=null. You have also made o=obj... by doing this both the variables are unhooked from the object . so the output coming as null... Comment that line o=object; and see what happens
public class Demo{
Deletepublic static void main(String[] args) {
Test obj=new Test();
Test o;
o=obj;
System.out.println("before assigning null:" + o);
obj=null;
//o=obj; here why you are assigning again
System.out.println("After assigning null:" + o);
System.out.println("Obj pointing to : "+obj);
//obj.dis();
}
}
awsum post bro
ReplyDeleteDiagram was awsm man
ReplyDeleteDiagram was awsm man
ReplyDeleteThank you man !
ReplyDeleteGreat Post.....Thanx
ReplyDeletenice post !!!!
ReplyDeletetoo good friend
ReplyDeletevery good explanation!
ReplyDeletereally helpful thank you so much
ReplyDeleteSo clare, very well explained. Awsome diagrams. Thank you!
ReplyDeleteVery good and clear understanding . Thanks
ReplyDeletethank you for sharing useful post.
ReplyDeletejava programming tutorial
welookups
I have learn more about the new operator from this post..Thanks for this post!
ReplyDeleteYou there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. Java Tutorial
ReplyDeleteI read this blog, this is very informative content. If anyone interested to learn Java course. They can search for java course in Indore.
ReplyDeleteClear explanation of the 'new' operator in Java and its role in dynamically allocating memory for object creation. Informative content!if you want to know about Data analytics so visit here :
ReplyDeleteUnveiling Data Power: Business Advancement Through Analytics
That was interesting blog to read.
ReplyDeleteJava classes in Pune