Dónal
Dónal

Reputation: 187379

java bounded generic type definition

What's the difference between the following type definitions

<E extends Number>

and

<? extends Number>

Cheers, Don

Upvotes: 22

Views: 3274

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503090

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.

Upvotes: 22

Related Questions