以下代码段发出编译器错误。
public List<Long> test()
{
boolean b=true;
return b ? Collections.emptyList() : Collections.emptyList();
}
需要不兼容的类型:
List<Long>
找到:List<Object>
它需要一个泛型类型,例如,
public List<Long> test()
{
boolean b=true;
return b ? Collections.<Long>emptyList() : Collections.<Long>emptyList();
}
如果删除此三元运算符,例如,
public List<Long> test()
{
return Collections.emptyList();
}
或者如果它由类似的if-else
构造表示,
public List<Long> test()
{
boolean b=true;
if(b)
{
return Collections.emptyList();
}
else
{
return Collections.emptyList();
}
}
然后它编译得很好。
为什么第一个案例不编译?在 jdk-7u11 上测试。