tags:

views:

541

answers:

3

Hi,

What's the difference between the following type definitions

<E extends Number>

and

<? extends Number>

Cheers, Don

+14  A: 

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.

Jon Skeet
More formally, would it be true to say that the second one is a formal type parameter definition, whereas the first is simply a parameter definiton?
Don
Would it be true to say that you should use the second definition only when you need to refer to the type (via the 'alias' E)?
Don
I wouldn't like to say on the first comment without consulting the spec (and I'm off to bed in a minute). The second comment seems reasonable though. Check what Josh Bloch has to say in Effective Java - he probably covers it :)
Jon Skeet
Actually, the question occurred to me while reading Effective Java!
Don
A: 

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

Ken Gentle
A: 

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.

Julien Chastang