Why do Consumers accept lambdas with statement bodies but not expression bodies?

Viewed 6523

The following code surprisingly is compiling successfully:

Consumer<String> p = ""::equals;

This too:

p = s -> "".equals(s);

But this is fails with the error boolean cannot be converted to void as expected:

p = s -> true;

Modification of the second example with parenthesis also fails:

p = s -> ("".equals(s));

Is it a bug in Java compiler or is there a type inference rule I don't know about?

3 Answers
Related