Why does undefined Generics types of Collections defaults to Object even if bound to other class?
问题 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