Hi,
What's the difference between the following type definitions
<E extends Number>
and
<? extends Number>
Cheers, Don
Hi,
What's the difference between the following type definitions
<E extends Number>
and
<? extends Number>
Cheers, Don
This version:
<? extends Number>
can appear in a non-generic method/type, and it basically means "I don't care what the type is, so long as it derives from Number
. I'm not going to really use the type, I just need it to be appropriate."
This version:
<E extends Number>
requires E to be a type parameter. It allows you to do more (for instance, creating an ArrayList<E>
later on) but the extra type parameter can make things more complicated when you don't really need them to be.
As an expansion on @Jon Skeet's answer, see
Which types are permitted as type parameter bounds?
in the excellent resource Java Generics FAQ from Angelika Langer
For all Java Generics related questions, I highly recommend "Java Generics and Collections" by Maurice Naftalin, Philip Wadler.
Angelika Langer's FAQ is OK, but it is also huge and somewhat rote.