Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

When to use wildcards in Java Generics?

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

public <T extends Animal> void takeThing(ArrayList<T> list)

Does the same thing as this:

public void takeThing(ArrayList<? extends Animal> list)

So here is my question: if they are exactly same, why don't we write

public <? extends Animal> void takeThing(ArrayList<?> list)

or

public void takeThing(ArrayList<T extends Animal> list

Also, when would it be useful to use a ? instead of a T in a method declaration ( as above ) with Generics, or for a Class declaration? What are the benefits?


Thanks!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This:

siddharth das wrote:public <? extends Animal> void takeThing(ArrayList<?> list)


and this:

siddharth das wrote:public void takeThing(ArrayList<T extends Animal> list)


both do not compile.

The first one doesn't because it's simply a syntax error - after the 'public' you can specify one or more type arguments between < and >, and a wildcard is not a type argument. A wildcard is not some kind of anonymous type argument.

The second one doesn't because you didn't specify that T is a type argument, so the compiler will expect that there is a type named 'T', which there isn't (unless you have a class T { ... } or interface T { ... } in scope).

A bit more about the second: Note that the convention is that type arguments are named with a single, upper-case letter, but this is just a common convention - the name of a type argument is just a name, and can be anything. If you don't specify the type argument between < and > after 'public', then the compiler cannot know if T is a type parameter or a concrete type. An example to make it more clear:

 
siddharth das
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then where to use T and where to use ?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on what the method does. In some cases the method needs to know the type, so then you have to use a type parameter, and in other cases it doesn't need to know the type so you can use a wildcard.

You can find everything you ever wanted to know about Java generics in Angelika Langer's Java Generics FAQ.
 
You can't have everything. Where would you put it?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic