• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Generic question with ?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code Fragment: List <?> l = new ArrayList <Integer>();

Why nothing can be added to this list? For example it's a compile error for l.add(new Object());

if List <? extends Object> l = new ArrayList <Integer>(); Why it's still a compile error for l.add(new Integer());

Is it a rule that if generic type definition has a ?, then no object can be added to the collection, but other operations still work?

Thanks.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by L Yan
Is it a rule that if generic type definition has a ?, then no object can be added to the collection, but other operations still work?





Since we don�t know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection.

When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don�t know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.

-------------------------------------------------------------------------

List <? extends Object> is an example of bounded wildcard. The ? stands
for an unknown type. However, in this case, we know that this unknown type is in fact a subtype of Object. We say that Object is the upper bound of the wildcard.
It seems that compiler will allow adding any object in this case as ny object is subclass of Object.

But its wrong. Still u can't add nything here except null.

Take this case...



The type of the second parameter to shapes.add() is ? extends Shape - an unknown subtype of Shape. Since we don�t know what type it is, we don�t know if it is a supertype of Rectangle; it might or might not be such a supertype, so it isn�t safe to pass a Rectangle there.

regards
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If u find problem understanding my explanation then u need to understand bout wildcard first.


One such link i m giving below...


wildcard in generics


regards
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Code Fragment: List <?> l = new ArrayList <Integer>();

Why nothing can be added to this list? For example it's a compile error for l.add(new Object());

if List <? extends Object> l = new ArrayList <Integer>(); Why it's still a compile error for l.add(new Integer());



List <?> is EXACTLY the same as List <? extends Object>

Also, when you say "<? super AddMe>", this typically means adding AddMe obj is possible into the list that's passed in the method like this:

void PassAnyList(ArrayList <? super AddMe> a){
AddMe b =new AddMe();
a.add(b);
}

And when you see extends instead of super, then you could say it's a rule that you can't add anything to the list passed in. So if you see List <?>, which actually means <? extends Object>, then because of this rule, you can't add anything to the list passed in.

See my response here for more.
[ June 10, 2006: Message edited by: Firas Zureikat ]
 
L Yan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply.

Is it also a rule that List<? super String> can only take a String?

List<? super String> list = new ArrayList <String> ();
list.add(new Object());

Why this code cannot compile? Is it always or sometimes that List <? super MyClass> can only add MyClass but not MySuperClass (MyClass extends MySuperClass)?
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using <? super rightHandOperantType>, means you could only add obj instances of the rightHandOperantType (and it's subtypes).

Since you are asking about <? super String>, this means you could add only String objects to the list.

So ofcourse list.add(new Object()) results in compiler error would come up because String is in your right hand side and not object.
[ June 10, 2006: Message edited by: Firas Zureikat ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic