【发布时间】:2011-10-12 10:54:26
【问题描述】:
您能否解释一下为什么第一个返回类型无法编译代码?
消息是: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);
}
}
}
【问题讨论】: