What happens when you create a new object?

前端 未结 10 1142
一个人的身影
一个人的身影 2021-02-10 18:53

Ok, so what happens when you do this.

A a1=new A();

A a2=new A();

A a3=new A();

I upload two pictures on how I imagine it being like. Can you

相关标签:
10条回答
  • 2021-02-10 19:07

    No, the second picture is true. Because each time you create a new object with a separate reference, it becomes a separate pointer to a separate instance in memory.

    A a1 = new A(); // a new instance of A is created in memory and can be referenced by a1.
    A a2 = new A();
    

    Even though a1 and a2 are of the same type, it does not mean that they point to, or reference, the same object instance in memory.

    But, if a1 = a2; is ever executed, now, both a1 and a2 reference or point to the same A object instance in memory.

    0 讨论(0)
  • 2021-02-10 19:15

    Go through the images, hope this helps.

    Refer Image

    Refer image

    0 讨论(0)
  • First picture is true for Strings( due to string pooling, an optimisation for strings known to be identical at compile time):

    String a1="s";
    String a2="s";
    String a3="s";
    

    The second one is true for:

    A a1=new A();
    A a2=new A();
    A a3=new A();
    

    If you want behaviour like String has - you should implement Flyweight pattern.

    0 讨论(0)
  • 2021-02-10 19:23

    The second picture is true. Each time you write new keyword following by constructor the new instance of class is created. Each time you write someVariable = ... the result of the expression from the right is assigned to the variable, i.e. the new reference is created.

    So,

    A a1 = new A(); // creates one object and assigns reference to a1
    A a2 = new A(); // creates another object and assigns reference to a2
    A a3 = a2; // just create yet another reference to previously created object 
    a1 = new A(); // creates new object and assigns its reference to the same variable that was previously used, so the previous object is "lost" now and can be garbage collected. 
    
    0 讨论(0)
  • 2021-02-10 19:23

    The second picture is correct. Each of the three statements is creating a reference (A a1 =), and an actual object in memory (new A()). Throw out the first picture :)

    0 讨论(0)
  • 2021-02-10 19:25

    new A() will call the no param constructor of class A and will create a new memory object.

    A a1=new A();  // new memory object created
    
    A a2=new A(); // new memory object created
    
    A a3=new A(); // new memory object created
    

    There are three different objects that are getting created, so the SECOND picture is true.

    For the FIRST picture to be true, the declaration of references should be something like this:

    A a1=new A(); // new memory object created
    
    A a2=a1; // a2 points to the same memory object as a1 does
    
    A a3=a1; // a3 points to the same memory object as a1 does
    

    Here only one object(new used only once) is created but all the three references point to it.

    0 讨论(0)
提交回复
热议问题