The following Java code fails to compile:
@FunctionalInterface
private interface BiConsumer<A, B> {
void accept(A a, B b);
}
private static void takeBiConsumer(BiConsumer<String, String> bc) { }
public static void main(String[] args) {
takeBiConsumer((String s1, String s2) -> new String("hi")); // OK
takeBiConsumer((String s1, String s2) -> "hi"); // Error
}
The compiler reports:
Error:(31, 58) java: incompatible types: bad return type in lambda expression
java.lang.String cannot be converted to void
The weird thing is that the line marked "OK" compiles fine, but the line marked "Error" fails. They seem essentially identical.
{ }
oftakeBiConsumer
... and if so, could you give an example ... if I read this correctly,bc
is an instance of the class/interfaceBiConsumer
, and thus should contain a method calledaccept
to match the interface signature ... ... and if that is right, then theaccept
method needs to be defined somewhere (eg a class that implements the interface) ... so is that what should be in the{}
?? ... ... ... thanks(String s1, String s2) -> "hi"
is an instance of BiConsumer<String,String>.