Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

java - Is List<?> the common parent of List<Integer> and List<Number>?

From this Oracle tutorial,

Although Integer is a subtype of Number, List<Integer> is not a subtype of List<Number> and, in fact, these two types are not related.

The common parent of List<Number> and List<Integer> is List<?>.

My question is about the second sentence. How can we say that List<?> is the common parent of List<Number> and List<Integer>?

? stands for an unknown type, which could be any reference type. Even if I say that ? would be Object here, Object being the common parent of Integer and Number does NOT mean that List<Object> becomes a common parent of List<Integer> and List<Number>.

question from:https://stackoverflow.com/questions/38009060/is-list-the-common-parent-of-listinteger-and-listnumber

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The context you need to understand is not of Integer or Number but it's the List. Let's suppose you were the one creating the List class then how would you create the class so it will only support a specific type of class.

Yes, that List class won't use Object as its type but instead use a wild card ?.

As documentation of WildCards say

So what is the supertype of all kinds of collections? It's written Collection<?> (pronounced "collection of unknown")

Same thing can be said for List.

So what is the supertype of all kinds of Lists? It's written List<?> (pronounced "List of unknown")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...