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

Difference between ? and T/E in generics

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

I would like to know the difference between ? and T/E.
Example:
I have a below method .




so i would like to know the difference between these 2 declaration.

 
Marshal
Posts: 75644
354
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have managed to post in the wrong location; this forum is for discussing the website itself. I shall try moving you to a more appropriate location.
 
Saloon Keeper
Posts: 13849
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Samir. The wildcard ? is used to tell the compiler you really don't care what kind of list of numbers you get, as long as you can pull numbers out of it. The user is then free to input a List<Double> or a List<Integer> or whatever. When you use List<T extends Number>, you can also accept different kinds of lists of numbers, except you're telling the compiler that you actually care about the exact type, because you either want to use that type in your return value, or you want to make sure the list corresponds to the class' type argument.

An example:

With this class, if we have Cage<Feline> felineCage and List<Lion> lions, we can do felineCage.addAnimals(lions);

The T in the above code is used to express that we really care that our cage is filled with Felines. The ? is used to express that we really don't care what kind of collection of animals we add to the cage, as long as they're Feline.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic