Re: A problem regarding generics
On 6/6/2010 5:38 PM, markspace wrote:
Daniel Pitts wrote:
List<? extends Object> list = new ArrayList<String>(); // compiles fine
List<? extends Object> list = new ArrayList<String>();
This says "A reference to a List which can have any Object retrieved,
but only objects of an unknown type can be added, is assigned a List
which can only have Strings added and retrieved. "
Small note: Since Object is the upper bound of all objects, <? extends
Object is the same as just <?>:
List<?> list = new ArrayList<String>();
And of course for either both <?> and <? extends Object>, any generic
type on the right hand side is compatible and can be assigned.
Also, "only objects of an unknown type can be added" isn't quite
correct. Since the type is unknown, you can't add any types, period.
(Except possibly null, I'd have to check.) You can only remove Objects.
You can't have an untyped reference, so I don't think the distinction is
useful.
Let me know the result of your test, I'd be interested in seeing it, and
I'm too lazy/busy right now to right my own SSCCE.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>