In Java type arguments, does <? extends E> mean

2019-04-23 11:03发布

In Java type arguments, does mean strictly subtypes only? or would E also suffice?

3条回答
Summer. ? 凉城
2楼-- · 2019-04-23 11:43
List<? extends Animal> animalList=new List<Dog>();
List<? extends Animal> animalList=new List<Animal>();

Both the lines compile without any error. Any function taking the list as a parameter understands that the objects in the list are of type E or a subtype of E.

查看更多
贪生不怕死
3楼-- · 2019-04-23 11:48

Yes, super and extends gives inclusive lower and upper bounds respectively.

Here's a quote from Angelika Langer's Generics FAQ:

What is a bounded wildcard?

A wildcard with an upper bound looks like ? extends Type and stands for the family of all types that are subtypes of Type , type Type being included. Type is called the upper bound.

A wildcard with a lower bound looks like ? super Type and stands for the family of all types that are supertypes of Type , type Type being included. Type is called the lower bound.

查看更多
相关推荐>>
4楼-- · 2019-04-23 11:49

It's not strict; E would suffice.

查看更多
登录 后发表回答