Why is Java Lambda also called Closures

This question already has an answer here:

  • What is the difference between a 'closure' and a 'lambda'? 10 answers

  • Those are two different terms that happen to often be mentioned in the same context.

  • A lambda is basically just an anonymous function. Example: () -> System.out.println("Hello") . It's a function, but it doesn't have a name.

  • A closure is a term regarding scoping. When you for instance refer to a local variable inside a lambda, as follows

    int localInt = 17;
    
    saveThisFunction(() -> System.out.println(localInt));
    

    you create a closure to capture localInt inside the lambda. Textually it looks obvious that it should be ok to access localInt inside the lambda, but keep in mind that the lambda can be stored and called long after the localInt has been popped from the stack.

  • So, creating a lambda expression often requires creating a closure (implicitly).


    Technically it's wrong, lambda expressions and closures are two slightly different things.

    Lambdas are anonymous functions, that in the Java world take the form of anonymous single method classes (see also functional interfaces):

    Runnable r1 = () -> System.out.println("I'm Runnable");
    

    Closures are a specific sub-type of lambda expressions where local variables have been binded to variables defined in a specific enclosing environment.

    In the Java world, you are just writing something similar to this example from here:

    final int x = 99;
    
    Consumer<Integer> myConsumer = (y) -> 
    {
        System.out.println("x = " + x);
        System.out.println("y = " + y);
    };
    

    For a more complete abstract definition of closures:

    Operationally, a closure is a data structure storing a function together with an environment: a mapping associating each free variable of the function ( variables that are used locally, but defined in an enclosing scope ) with the value or storage location the name was bound to at the time the closure was created.

    A closure—unlike a plain function—allows the function to access those captured variables through the closure's reference to them, even when the function is invoked outside their scope.

    Source

    And that last part means that you can do this:

    public class Funct{
    
    
        public static Consumer<Integer> getThatClosure(){
            final int x = 99;
    
            Consumer<Integer> myConsumer = (y) -> 
            {
                System.out.println("x = " + x);
                System.out.println("y = " + y);
            };
            return myConsumer;
        }
    
        public static void main(String... args){
            Consumer<Integer> cons=getThatClosure();
            cons.accept(0);
        }
    
    } 
    

    With this output:

    x=99
    y=0
    

    simply because the terms are equivalent (I think as well as "anonymous function") and this is how similar mechanism is being called in different languages, eg Groovy has closures

    btw, I don't think this is right board to ask such questions

    链接地址: http://www.djcxy.com/p/51272.html

    上一篇: 在GO和局部变量中关闭

    下一篇: 为什么Java Lambda也称为闭包