• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

about generics

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collection<?> c=new ArrayList<String>();

we cannot add anything to c.Then what is the use of referring object to c??
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abdul Mohsin:
so that we can pass " Collection<?> c" into method which is taking Collection<Integer> or Collection<Float>...


Abdul Mohsin



NO Abdul,

A method asking for Collection<Integer> can only take Collection<Integer> of
any subtype of base type(Collection) but only parameterized with Integer. Nothing else!

And same as if method asking for Collection<String> can only take Collection<String> of any subtype of base type(Collection) but only parameterized with String. Nothing else!



Sampath posted:
Collection<?> c=new ArrayList<String>();
we cannot add anything to c.Then what is the use of referring object to c??



Don't get confused with what c can refer to and what we can add to c;
We have flexibility of referencing any Collection object parameterized with any type in this case. c can refer to all. But you can't use add() method to it. Only null can be added though.
[ May 04, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

next time I will first think and then post.

Abdul Mohsin
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic