0

I'm trying to understand functional interfaces and lambda functions.And I can't find an explanation of how a function in a functional interface connects it with a lambda function , that is, let's say there is such a functional interface

@FunctionalInterface
interface MyPredicate {
    boolean test(Integer value);
}

and now we assign the variables of the functional interface to the lambda function:

public class Tester {
    public static void main(String[] args) throws Exception {
        MyPredicate myPredicate = x -> x > 0;
        System.out.println(myPredicate.test(10));   //true
    }
}

I'm exactly wondering why when calling myPredicate.test(10) a call is being made x > 0. That is, do I understand correctly that when we assign a lambda function, the compiler somehow connects the function from the functional interface with the body of the lambda function?it's just that inheritance and override are usually used for this ,but here the compiler does it or how?I will be glad to have explanations to understand this issue

Omegon
  • 387
  • 2
  • 10
  • The functional interface has exactly one unimplemented method. The Lambda implements exactly one method. – Turing85 Jun 28 '21 at 14:30
  • Does this answer your question? [Why Functional Interfaces in Java 8 have one Abstract Method?](https://stackoverflow.com/questions/23342499/why-functional-interfaces-in-java-8-have-one-abstract-method) Also [this one](https://stackoverflow.com/a/36882003/7804477) has a few other details as well. – Gautham M Jun 28 '21 at 14:32
  • @Turing85 but if lambda is a functional interface, then it can't have an implementation,isn't it? – Omegon Jun 28 '21 at 14:36
  • The lambda expression is not a functional interface, it **implements** the functional interface. – Jesper Jun 28 '21 at 14:38
  • *"but if lambda is a functional interface"* ... that's not what Turing85 wrote, they wrote "The Lambda **implements** exactly one method". – Tom Jun 28 '21 at 14:38
  • @Omegon `x > 0` would be the implementation. Assume you have class that implements `MyPredicate` that overrides the `test` with `return x > 0`, and then you create an object of this class and invoke `test(10)` for this object. Using a lambda `x -> x > 0` is equivalent to this, but avoids the need of explicitly creating an implementing class – Gautham M Jun 28 '21 at 14:42
  • @GauthamM okey,now it's more understandable, I just thought that there is an explanation of how this transformation (or binding) occurs. – Omegon Jun 28 '21 at 14:47

1 Answers1

0

What exactly IS a lambda expression in pre-Java8 terms?

It's an instance of an anonymous class implementing the interface needed in the current context.

So, to understand

MyPredicate myPredicate = x -> x > 0;

we have to consider some different aspects.

Here, the lambda expression x -> x > 0 is required to give a result compatible with MyPredicate. So, by considering the context where the expression is used, it is equivalent to

MyPredicate myPredicate = new MyPredicate() {
    boolean test(Integer whateverName) {
        // some method body
    }
};

Now, the lambda expression fills the body of that method. The x -> part defines the parameter name, so now we have

MyPredicate myPredicate = new MyPredicate() {
    boolean test(Integer x) {
        // some method body
    }
};

And the x > 0 defines the body and the value to be returned:

MyPredicate myPredicate = new MyPredicate() {
    boolean test(Integer x) {
        return x > 0;
    }
};

So, when you call myPredicate.test(10), there's nothing special going on. The (anonymous-class) instance in myPredicate gets its test() method called with argument 10. It's only that this instance has been created using the lambda-expression syntax.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7