Why does undefined Generics types of Collections defaults to Object even if bound to other class?

放肆的年华 提交于 2019-12-11 03:17:22

问题


class Test<G extends String>{
    public G test(){return null;}
    public List<G> tests(){return new ArrayList<>();}
}
public void doTest(Test t){
    //works fine
    String str = t.test();
    //Compile error: expected String found Object
    str = t.tests().iterator().next();
}

I would like the last line to return a String instance instead of Object, as the type G was bound to subclass String. There is any other way than casting ?


回答1:


No, the type as been erased. You're running into two different rules for type erasure - from JLS section 4.6:

  • The erasure of a parameterized type (§4.5) G is |G|.
  • The erasure of a type variable (§4.4) is the erasure of its leftmost bound.

So the erasure of List<G> is List, but the erasure of G is String - that's why the first assignment works.

All you need to do to get this code to compile is use a wildcard in your parameter:

public void doTest(Test<?> t)


来源:https://stackoverflow.com/questions/24171300/why-does-undefined-generics-types-of-collections-defaults-to-object-even-if-boun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!