2

Could someone please explain me why am I getting this output

package code;
import java.util.ArrayList;
class A {
}
class B extends A {
}    
class C extends B {
}
public class ThreadDemo {
    public static void main(String[] args) {
        ArrayList<A> x = new ArrayList<A>();
        ArrayList a = new ArrayList();
        x.add(new A());
        a = x;
        a.add(new B());
        ArrayList b = a;
        ArrayList<C> c = (ArrayList<C>) b;
        c.add(new C());
        a.add(new A()); // I am not sure why object of A is also getting printed at the last
        for (Object obj : c) {
            System.out.println(obj + " class Name " + obj.getClass().getName());
        }
    }
}

Output :

----------
code.A@7852e922 class Name code.A
----------
code.B@4e25154f class Name code.B
----------
code.C@70dea4e class Name code.C
----------
code.A@5c647e05 class Name code.A
----------
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Onic Team
  • 1,620
  • 5
  • 26
  • 37
  • 1
    I'm getting confused with the output lastLine – Onic Team Jun 17 '17 at 11:55
  • 2
    You are only *creating* two `ArrayList` instances and the second one from `ArrayList a = new ArrayList();` is never used, because it is discarded two lines down with value of `a` is replaced. After that, `x`, `a`, `b`, and `c` are all referecing the *same* `ArrayList` object. – Andreas Jun 17 '17 at 12:01
  • If that's not the output you expect, what *do you* expect, and why? – Andy Turner Jun 17 '17 at 12:03
  • You need to clarify your question, because *"explain me why am I getting this output"* does not explain exactly what part of the output you're confused about. The duplicate link explains why your output prints "classname@hexcode". – Andreas Jun 17 '17 at 12:04
  • @AndyTurner It seems OPs *main* question is the comment in the code, i.e. *"why object of A is also getting printed at the last"*. Question needs to be cleaned up to clarify that that is the question. Both Mureinik (see his [original answer](https://stackoverflow.com/posts/44604315/revisions) and I (see [dup link](https://stackoverflow.com/q/29140402/5221149)) thought question was about the `@hashcode` in the output. – Andreas Jun 17 '17 at 12:05
  • @Andreas You got it please clarify – Onic Team Jun 17 '17 at 12:08
  • @VedPrakash It's *your* question. You clarify it. – Andreas Jun 17 '17 at 12:08
  • @Andreas I meant,if you know the answer,please do let me know. – Onic Team Jun 17 '17 at 12:10
  • @VedPrakash I [already did](https://stackoverflow.com/questions/44604283/could-someone-please-explain-me-why-am-i-getting-this-output-in-below-program?noredirect=1#comment76195788_44604283), and Mureinik changed [his answer](https://stackoverflow.com/a/44604315/5221149) to explain too. – Andreas Jun 17 '17 at 12:12
  • @VedPrakash, The answer is Your arrayList contains the object of class `A` as the last element and hence it is printed. You making `a =x` leaves you with **One and only one** object of ArrayList. You add all the elements in it and then while printing you get what you added. I have mentioned the same in my answer below. – nits.kk Jun 17 '17 at 12:15

4 Answers4

2

a and c are the same object. Let's take a look at the relevant lines of the code:

ArrayList a = new ArrayList(); // a is created
ArrayList b = a; // b holds a reference to the same object
ArrayList<C> c = (ArrayList<C>) b; // and so does c

So whenever you add an object to c, it will be present in a too (since, as noted, they are the same object!).

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Because when you use b=a you are just saving a reference to a in b. Whatever change you apply to a will also affect b. And since you did c=b then c will get affected too. You are not saving a copy of the list then changing the value of one of them, instead you are saving a reference to a in b and c.

So when you used a.add() at the end, it was added to b and c too

Mohd
  • 5,523
  • 7
  • 19
  • 30
1

In your code you have 4 references of ArrayList : x,a,b and c.

You initially have 2 objects of ArrayList with a being the reference to one and x being the reference to the other one. But a = x makes the object of ArrayList initially referenced by a eligible for garbage collection. Now both a and x refers to same one ArrayList and you already added an object of A. Hence ArrayList has [A]

Next you add an object of B to it so status of arrayList is [A,B]. Both a and x are referring to this list. Next you create another reference b and make it also refer to the object referred by a so now you have a,b,x referring to the same list.

Next you create another reference c and make it refer to same object as refferred by b so now you have 4 references referring to same list. Status of list is [A,B].

Now you add object of C in the list using reference c followed by adding an object of A using reference a. Since all the 4 references refer to same one ArrayList hence objects of A and B both gets added in the list and status of list : [A,B,C,A].

Now when you iterate the list and in System.out.println() use the reference to print , the toString() method gets invoked and as you have not overriden it hence the method as derieved from Object.java gets invoked.

The answer to your question , as why object of A gets printed in the last ? : Because the list contains it in the last, as explained above. Hope it helps.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
1

you are assigning the first ArrayListobject to all the ArrayList references.

ArrayList<A> x = new ArrayList<A>();
ArrayList a = new ArrayList();
//x.add(new A());
a = x;    //now a & x refer to same object.
//a.add(new B());
ArrayList b = a;    //b refer to the same object.
ArrayList<C> c = (ArrayList<C>) b;   //now a,b,x & c every reference refer the same object.

Thus you are assigning elements to the same ArrayList throughout the code.

Hope this would be helpful......

LSampath
  • 138
  • 1
  • 3
  • 11