StK*_*ler 12 java generics casting
你能解释为什么第一个返回类型的代码无法编译?消息是:Type mismatch: cannot convert from List<capture#1-of ? extends Object> to List<String>.
在第二种情况下是否插入了显式转换?
public class GenericsTest {
private String getString() {
return null;
}
public List<String> method() {
String someVariable = getString();
//first return type
//return someVariable == null ? Collections.emptyList() : Collections.singletonList(someVariable);
//second return type
if (someVariable == null) {
return Collections.emptyList();
} else {
return Collections.singletonList(someVariable);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2250 次 |
| 最近记录: |