• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics and wildcards in java

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The intention of wild cards in java, for the benefits of method parameters? There is no point in Constructing java collections with the wild cards(we may use <? super T>.

Please somebody help!
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't understand your confusion properly but I hope that you know that you can add elements to a list of type <? super T>...
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:
The intention of wild cards in java,


The intention is to put upper or lower boundaries on the type checking that generics provide.

Abimaran Kugathasan wrote:
for the benefits of method parameters?


If you are asking about generic collections as method parameters, the benefits of wildcards here is as above. You can put upper or lower boundaries on the types allowed inyour collection
If you are asking about generic methods
look at this:

this is nice because you can accept a collection of any type (T) and you have a generic method, you can do operations using T as a variable meaning, "whatever collection c is full of, that is the type we mean"


Abimaran Kugathasan wrote:
There is no point in Constructing java collections with the wild cards(we may use <? super T>).


The point of making an ArrayList<? super Child> you want to allow any ancestor of Child into your ArrayList
or ArrayList<? extends Car> : You want to limit your list to anything that is a car. could be Porche, Pinto, Pontiac, Plymouth, Prius


does this address your question?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim McGuire wrote :


The point of making an ArrayList<? super Child> you want to allow any ancestor of Child into your ArrayList
or ArrayList<? extends Car> : You want to limit your list to anything that is a car. could be Porche, Pinto, Pontiac, Plymouth, Prius



My doubt :


So the intention of the wildcard is to used with method? Not constructing? Please clear my doubt!

Thanks in Advanced!
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement which you are making can be good for your own understanding. But you can not hardcore this statement.

Thing which you should remember is that if whildcard is used with extends keyword like

then you can not add anything to the list even if you are using whildcard with extends keyword in the parameter to the function.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's why I'm asking... What is the point of this declaration?

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic