Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
cmbhatt
Collection<?> c = new ArrayList<String>();
In this statement, <?> on the left side means this collection can hold anything...(Object or any subtype)
and <String> on the right means that it can hold only strings.. Am I right?
So, what does this complete statement means..
Collection<?> c = new ArrayList<String>();
Please explain.
cmbhatt
Originally posted by Chandra Bhatt:
Posted By Lovleen Gupta
Hi Guptajee,
Collection<?> c = new ArrayList<String>();
Collection is on the top of the interface heirarchy so you are availing polymorphism on base type as the term mostly used in K&B book.
Collection and ArrayList are base type so you are able to use Collection reference variable to hold the reference of ArrayList. ArrayList implements List that extends Collection.
Now come to the original issue:
wildcard <?> means unknown type, it can be Object, Animal, String, Dog, Poodle, String, StringBuffer or so.
Always keep in mind when we talk about what the reference variable can hold is not what about what objects we can add using the reference variable in collection.
Collection<?> means it can hold any collection object parameterized with any type (that is not known to us for now).
Collection<?> mycol =null;
mycol = new ArrayList<String>(); /*mycol refers to ArrayList paramerized with String */
mycol = new LinkedList<Animal>(); /*mycol refers to LinkedList paramerized with Animal */
mycol = new Vector<Double>(); /*mycol refers to Vector paramerized with Double */
Restriction What you can't do in the case of <?> (unknow type) is to add any object to it except null (ofcourse null is not object and it can be substituted to any object)
mycol.add(null); //OK
mycol.add("hello"); //NO NO
mycol.add(120.43); //NO NO
mycol.add(new Animal()); //NO NO
One should not confuse with that the reference variable can hold, with what can be added to the collection.
When we are allowed to add :
Collection<? super Dog> mycol = null; //mycol can only hold the reference of any collection that implements Collection, ofcourse and parameterized with Dog or superclass of Dog, nothing else
mycol = new ArrayList<Dog>(); //OK
mycol = new ArrayList<Animal>(); //OK
mycol = new ArrayList<Object>(); //OK, Object class is Daddy of all classes
mycol = new ArrayList<Poodle>(); //NO NO NO
What can we add to this collection:
mycol.add(new Dog()); //Good
mycol.add(new Poodle()); //Good
mycol.add(new Animal()); //No no, if your mind asks why can't we add //Animal? goto top and read from the scratch;
//otherwise you are in good shape
Hope this helps you? Do I miss anything?
Thanks and Regards,
cmbhatt
I am good with what a reference variable can hold but am still confused with what you can add to it.
I guess, K&B also doesn't go much into add() method except saying that in wildcards can't add anything to it because of the fear that you may add something wrong to it. Though, it does say that when using the keyword super , you may be able to add.
Please explain about using add with wildcards a bit more.
cmbhatt
Originally posted by Chandra Bhatt:
Hi Guptajee,
Ok you got the the point what a reference variable can hold and what can't;
You already have info of, why collection prohibits you to add anything else,
"because you might add anything wrong" : "I like this phrase of K&B"
If I have to tell you in one line about what can be added, if you are allowed yo add Dog, OK! So there is nothing to stop you from anything that is subclass of Dog. This should be crammed to the mind.
In case of wild card, because there is not information regarding what can be added to it, so there can't be added anything to it; but I am sure you wont be confused with adding and assigning an collection object to the reference variable. add() is different and reference assignment is different.
The problem you already know the Cat and Dog, given in detail in the K&B I need not to explain it. What problem there was supposed to occur, you better revise from there if required.
Dont forget, in last three lines we are only concerned with what a reference variable alist can hold. But you should know, you can't add anything to the collection, not even Dog, NOT AT ALL. Compiler does not know what can be subclass of Dog of extends Dog. If you are dead concerned with adding to alist, I can tell you to do it through casting.
Here we are explicitely telling the compiler what we are going to add, so no problemo! Compiler is happy. We did't break the rule here; the rule that we can't add anything to alist; It is still true that we can't add anything to it, but without casting.
Compiler doesn't have any problem when we add the subclass while Dog is required. Because there is no harm.
Yeah Guptajee, now do you feel comfortable? Let me know![]()
Thanks and Regards,
cmbhatt
[ March 31, 2007: Message edited by: Chandra Bhatt ]
ArrayList<? super String> alist = new ArrayList<String>();
//Now alist reference variable can hold any ArrayList that is parameterized with String or superclass of String, (ofcourse Object class). But the upper bound is String so only and only you are allowed to "add" String object in the list.
alist.add("a");
alist.add(new String("bb");
cmbhatt
Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%