我正在阅读关于Java和Kotlin之间差异的kotlin文档 https://kotlinlang.org/docs/reference/comparison-to-java.html。据说 Kotlin没有通配符类型。但仔细阅读泛型文档后,我发现了相关信息 star-projection老实说,我无法看到差异。有人可以向我解释这是怎么回事。
List<?> list;
与此不同
lateinit var list: MutableList<*>
答案 0 :(得分:11)
Probably what it means is that Kotlin doesn't have wildcards in the same form as they are present in Java. Instead, Kotlin uses a different concept of mixed-site variance, which adds an option to specify the variance at declaration-site, that is something you cannot do with Java wildcards.
As to the star-projection, it is actually almost equivalent to an unbounded Java wildcard. However, there is at least one difference related to the Kotlin type system.
When you have a List<?>
, the type is unknown to Java, but all Java referential types are nullable, and therefore you can safely pass a null
as an argument of the unknown type, e.g. list.add(null)
.
In Kotlin that is not true, a star projection MutableList<*>
combines both out Any?
and in Nothing
projections, and the latter means that you cannot pass anything at all to the methods where the type is unknown (Nothing
is the type that has no value).
The same can be said of the out-projections: while you can pass a null
as a Java bounded wildcard type ? extends T
, you cannot do the same with Kotlin out T
projection.
And the exact equivalent of the Java unbounded wildcard is <in Nothing?>
, because it lets you pass a null
as an argument of the unknown type (Nothing?
is Nothing
∪ { null
}, this is the type that has only null
value).