传递给方法的无界通配符
public class ColTest {
static<T> T wildSub(ArrayList<? extends T> holder, T arg){
T t=holder.get(0);
return t;
}
public static void main(String[] args) {
ArrayList<?> list=new ArrayList<Long>(Arrays.asList(2L,3L,7L));
Long lng=1L;
ColTest.wildSub(list, lng);
}
}
真的很感兴趣为什么这个片段是合法的,因为 wildSub 的签名仅采用 T
的 ArrayList
或派生自 T
以及 类型的 arg >T。但是
意味着 - 某种特定类型,未知,它如何满足编译器的要求?毕竟类型
并不意味着
...
public class ColTest {
static<T> T wildSub(ArrayList<? extends T> holder, T arg){
T t=holder.get(0);
return t;
}
public static void main(String[] args) {
ArrayList<?> list=new ArrayList<Long>(Arrays.asList(2L,3L,7L));
Long lng=1L;
ColTest.wildSub(list, lng);
}
}
Really interested why this snippet is legal, because the signature of wildSub takes only ArrayList
of T
or derived from T
, and arg of type T
. But <?>
means - some specific type, not known, and how it can satisfy the compiler? After all type <?>
doesn't mean <? extends Long>
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编译器可以自由推断与参数类型和返回类型兼容的任何内容。在您的情况下,它始终可以将
T
推断为Object
。这将签名变成Which 意味着它可以将任何 ArrayList 作为第一个参数,将任何内容作为第二个参数。由于您不对返回值执行任何操作,因此
Object
就可以了。The compiler is free to infer anything that is compatible with the types of the arguments and return type. In your case it can always infer
T
asObject
. Which turns the signature intoWhich means it can take any ArrayList as first argument and anything as second. Since you don't do anything with the return value,
Object
will be okay.如果您考虑一下编译器在哪里使用 Object ?被使用,它为什么会编译是有道理的。这就是全部内容了。
如果您正在执行任何依赖于 ? 的操作作为某个类,如果传入了错误的类,您将在运行时收到强制转换异常。
If you think about it as the compiler using Object where ? is used, it makes sense why it would compile. That is all there is to it.
If you are doing any operations dependent on ? being a certain class, you will get a cast exception at run time if the wrong class is passed in.
这是由于捕获转换造成的。在内部,编译器将表达式
Foo
的类型转换为Foo
,其中X
是特定但未知的类型。This is due to capture conversion. Internally, compiler converts the type of an expression
Foo<?>
toFoo<X>
, whereX
is a specific albeit unknown type.作为现有(正确)答案的补充,使其更加清晰:
As an addition to existing (correct) answers to make it more clear: