• 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

Generic Bounded Type Parameters versus Upper Bounded Wildcard Confusion

 
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a little confused over generics and the difference between bounded type parameters and upper bounded wildcards. As I understand it, I can pass a ArrayList<Integer> or a ArrayList<Double> to a generic method that accepts a List<T extends Number> parameter. This is also true if the method accepts a List<? extends Number> parameter.

What, then, is the difference between the following two methods?



and

 
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is worth trying those methods using a Stream, now you can use Java8.You can simplify that with the Arrays class method which returns a Stream from an array.

******************

You won't notice the difference until you try adding something to the List. If you can work out what T is, you can add a T to the List. You cannot work out what ? is so you cannot add anything to a List<? extends Foo>
 
Saloon Keeper
Posts: 13891
314
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first example shouldn't compile. You're using the type parameter T, which has not been declared (since it's a static method), and you can also not put a bound on a type parameter in an argument list. The first method signature should read:

The difference between using type parameters and wildcards is that when you're using a wildcard, you're saying you don't care about what the actual type parameter is, because you don't need it. With a type parameter, you care about it because you're going to do something with it.

To calculate the sum of a list of numbers in double precision, you don't need to know exactly what the type of the elements is, just that they extend Number. The fact that you don't care is signified by the wildcard. If you can use a wildcard, use it.
 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sort of makes sense. So I might have a method that adds a Development<Flat> or Development<Shop> to a Collection<Development<T>. Since I don't really care what is in the collection of different developments, I would be best to declare the collection as where both Flat and Shop extend or implement Building. However, because a development can be one of several types I need to be more type specific and declare it as , rather than ?

I'm at work at the moment and had a spare bit of time so thought I'd start reading up on generics. I'll have a bit more of a play when I get home and can use a computer with a JDK on it and hopefully I will better understand the difference when I start trying the different concepts.
 
Campbell Ritchie
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go through the Java┬« Tutorials, where you will find at least two sections about generics. Also search for “Angelika Langer Java Generics FAQ”.
 
Ashley Bye
Ranch Hand
Posts: 146
2
Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It all made sense when I finally got to the section on Guidelines for Wildcard Use. I'd recommend a quick read of this page, especially the 4 wildcard guidelines between the two horizontal rules, to anyone struggling to understand how to apply the various generic parameters; understanding when they should be used might just make something tweak to understand the how. The suggested search was also very helpful in explaining a few of the concepts in a slightly different way to that in the Oracle Tutorial - thank you Campbell.

I don't for a minute believe that I am now a master on the subject but those 2 resources have certainly helped get me on the right track. Now to find some projects that I can start applying my new found knowledge to. Stand-by for more questions...!
 
Campbell Ritchie
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you search for “Java Buzzwords”, you find about 10 or 12 things which are supposed to describe Java. One is “simple”. That was of course in the days before generics
 
Stephan van Hulst
Saloon Keeper
Posts: 13891
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashley, have a cow for asking intelligent questions and knowing your limits and wanting to move beyond.
 
Campbell Ritchie
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And remember you can take things out of a Collection<?> or a Collection<?...> but you can't usually put things into it. You can put things into a Collection<T extends Something> however. So you can put things into a Development<T extends Building> e.g.
At least I think that would work. Since you have read the tutorials more recently than I have, you will know whether I have got it all wrong.
 
Campbell Ritchie
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree it was an interesting question.
 
today's feeble attempt to support the empire
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic