Java 为什么我可以对可运行接口和供应商功能接口使用相同的lambda?

Java 为什么我可以对可运行接口和供应商功能接口使用相同的lambda?,java,java-11,Java,Java 11,以下一段代码为例 private static int counter = 0; void some method() { Runnable a = () -> 1; // compilation error -> Bad return type - which is expected Runnable b = () -> counter++; // Here I am expecting to receive an error Supplier<In

以下一段代码为例

private static int counter = 0;

void some method() {
   Runnable a = () -> 1; // compilation error -> Bad return type - which is expected
   Runnable b = () -> counter++; // Here I am expecting to receive an error
   Supplier<Integer> c = () -> counter++; // this works - as expected
}
那么,为什么在第一个代码段的b行上没有编译错误呢?或者我应该说java如何知道将return语句放在哪里?只是通过查看函数接口方法的方法签名吗

只是通过查看函数接口方法的方法签名吗

这个。Java注意到lambda被分配给一个
可运行的
,并推断不应该有一个
返回

只是通过查看函数接口方法的方法签名吗


这个。Java注意到lambda被分配到一个
Runnable
,并推断不应该有
return

是的,现在有点意义了。我只是想确认一下。谢谢。是的,现在有点道理了。我只是想确认一下。非常感谢。

Runnable a = this::test; 
Runnable b = this::testInt;

void test() {
  counter++;
} 

int testInt() {
  return counter++;
}