In Java8, what is the type of the following lambda ???
() -> {}
That is a function that takes no arguments and returns nothing.
Stated differently:
public class A {
static void a(){}
static void main(String[] args) {
???? a = A::a
}
}
What should I replace the question marks with?
In Scala, this would be a Function0[Unit]
I think, but I don't find anything alike in Java.
Unlike Scala, lambdas in Java don't have a fixed type. Rather they take the type of whichever interface you declare the variable as, as long as that interface defines exactly one abstract method and that method has the same signature as the lambda.
So in your case, you want an interface that defines a void method that takes no parameters. Runnable
would be one example of that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With