You can't construct wildcard parametrized type, like this: new LinkedList<? extends Object>(). You must construct concrete parametrized type object or raw type object, like: new LinkedList<Object>() or new LinkedList()
and then Katrin concluded from this behaviour that a List<Object> and a List<? extends Object> is more or less the same thing.
No, it is not. Your list contains only objects (a String, an Integer, a Boolean and another String). Must compile, because you can store everything into an Object.
Compare with this:
This compiles also.
Back to your list of objects: You can put Strings, Integers etc inside easily, but what you get back from the list is an object. When you want eg to multiply the second entry with 3, then you first have to cast:
Hope that helped.
all events occur in real time
Joined: Mar 07, 2007
Thanks for your replies
I am a bit clearer why:
1.I can't use wildcards when creating objects:
2.I also understand that if you add to a List any types of objects i.e. String, Integer ... you get out Object and you need to cast.
What I am not clear about still is what diffference usind wildcards makes in the following example:
In my mind in both cases you would have added to the list various shapes (subclasses of Shape) but you would be getting out objects of type Shape, which you would print.
I would be very grateful if someone sheds a bit of light on this concept.