The following segment of code issues a compiler error.
public List<Long> test()
{
boolean b=true;
return b ? Collections.emptyList() : Collections.emptyList();
}
incompatible types required:
List<Long>
found:List<Object>
It requires a generic type like,
public List<Long> test()
{
boolean b=true;
return b ? Collections.<Long>emptyList() : Collections.<Long>emptyList();
}
If this ternary operator is removed such as,
public List<Long> test()
{
return Collections.emptyList();
}
or if it is represented by an if-else
construct like,
public List<Long> test()
{
boolean b=true;
if(b)
{
return Collections.emptyList();
}
else
{
return Collections.emptyList();
}
}
then it compiles fine.
Why doesn't the first case compile? Tested on jdk-7u11.