31
public List<String> foo1() {
    List<String> retval = bar();
    if (retval == null)
        return Collections.emptyList();
    else
        return retval;
}

public List<String> foo2() {
    List<String> retval = bar();
    return retval == null ? Collections.emptyList() : retval;
}

Why does foo1() compiles fine whereas foo2() has an error? (to be more precise "Type mismatch: cannot convert from List<capture#1-of ? extends Object> to List<String>")

I would have thought that both functions would compile to the same bytecode, so a clever compiler should infer the correct type for emptyList()...

7
  • 2
    Which Java version are you using? Commented Jun 18, 2014 at 15:23
  • They both compile just fine for me (Java 8U5).
    – Steffen
    Commented Jun 18, 2014 at 15:23
  • What is bar returning? Can you post the code? And is the error in Collections.emptyList() or retval? Commented Jun 18, 2014 at 15:23
  • 1
    @AnubianNoob Yes, List<String> bar(). But I don't think this is very relevant to the error. Commented Jun 18, 2014 at 15:42
  • 1
    That explains the problem... Commented Jun 18, 2014 at 15:44

2 Answers 2

27

Compiles for me fine in java 8.

Earlier versions of Java might need more help

return retval == null ? Collections.<String>emptyList() : retval;

should work.

EDIT This is due to improvements in Java 8 type inference as explained here

http://openjdk.java.net/jeps/101

And here's a blog with the highlights: http://blog.jooq.org/2013/11/25/a-lesser-known-java-8-feature-generalized-target-type-inference/

6
  • 1
    @LuiggiMendoza Java 8 improved it's type inferencing blog.jooq.org/2013/11/25/…
    – dkatzel
    Commented Jun 18, 2014 at 15:26
  • 2
    Ok, add that to your answer, otherwise looks like a long comment with some code. Commented Jun 18, 2014 at 15:26
  • @lpratlong it compiles in Java 8. As I posted in my comment in OP question: which Java version are you using? Commented Jun 18, 2014 at 15:27
  • @dkatzel Sorry, I'm stupid, I do not test the same thing :). Work fine: ideone.com/iun5CB Luiggi, no, its solution is for Java < 8. And it works, indeed.
    – lpratlong
    Commented Jun 18, 2014 at 15:29
  • I didn't knew the syntax Foo.<String>bar(). Is that something new to Java 8? Commented Apr 7, 2015 at 15:50
6

This is related with Type Inference from a generic method.

In case of code before ver. 8. It must be declared the type of result for this case.

return retval == null ? Collections.<String>emptyList() : retval;

Since ver. 8 notion of what is a target type has been expanded to include method arguments. So this is no longer required.

4
  • 1
    We do not accept link only answer. Please improve the answer or move it into a comment. Refer to What is an acceptable answer, section 12. Commented Jun 18, 2014 at 15:25
  • 2
    Not that I am longer than you here. But there is no point of rewriting the the origin page. It is more likely not that SO will expire than oracle ;-). Commented Jun 18, 2014 at 15:28
  • 4
    I know lots of Oracle links in other Q/As here that expired. So, it is always better to post the explanation here than just referring to external links. These links should be to get more info to backup the answer, not the answer. Commented Jun 18, 2014 at 15:30
  • Lifetime on SO means nothing. SO's rules of posting change almost by the day. Commented Jun 18, 2014 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.