Friday 19 April 2013

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 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'



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.

26 comments :

  1. Replies
    1. Dear 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.

      As 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)

      Delete
  2. very good explanation ,simple english...and nice work with figure....awsome job man..

    ReplyDelete
  3. in above code..i tried to implement your explanation but check after assigning null to obj then assigning to another ,all values r null.

    ReplyDelete
    Replies
    1. class XYZ{
      int 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

      Delete
    2. This comment has been removed by the author.

      Delete
    3. What he means to say is even when u say obj=null , the o variable still points to the xyz object....
      But 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

      Delete
    4. public class Demo{
      public 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();
      }
      }

      Delete
  4. really helpful thank you so much

    ReplyDelete
  5. So clare, very well explained. Awsome diagrams. Thank you!

    ReplyDelete
  6. Very good and clear understanding . Thanks

    ReplyDelete
  7. thank you for sharing useful post.
    java programming tutorial
    welookups

    ReplyDelete
  8. I have learn more about the new operator from this post..Thanks for this post!

    ReplyDelete
  9. You 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

    ReplyDelete
  10. I read this blog, this is very informative content. If anyone interested to learn Java course. They can search for java course in Indore.

    ReplyDelete
  11. Clear 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 :
    Unveiling Data Power: Business Advancement Through Analytics

    ReplyDelete