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

Confusion with generics wild card

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


Please explain it . What is the difference between the two methods show1() and show2() ???
When to use the wildcards ?
Thanks.
 
Bartender
Posts: 15720
367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You use wildcards when you don't care about the exact type argument, when they're not important for how the method performs.

Let's take two methods as an example:
The first method shuffles a list of elements. You don't really need to know the type of the elements in order to shuffle them, so you can use a wildcard to indicate this.

The second method declaration is more difficult. To sort a list of items, you *do* need to know something about those items: You need to know how they compare to one another, otherwise you can't sort them. So instead of a List<?> it needs a List<T>. So what is T?

T extends Comparable<? super T>. This looks difficult, but if you give it some thought it's not really. This part just says that the type is comparable to something - it doesn't matter what - that is a super-type of T.

Let's say you had a default way of comparing two pieces of fruit, for instance, by size. We also have a special type of fruit, pears:
Now, if we have a List<Pear> we can submit it to the sort() method to have them sorted by size. In this case, T is Pear, which works because Pear is comparable to some super type of itself, namely Fruit.
 
reply
    Bookmark Topic Watch Topic
  • New Topic