Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Java Lambda also called Closures [duplicate]

Tags:

java

java-8

I am currently going through the Java Lambda and find it is also called Closures.

Please is there a reason why it is also called Closures? I want a technical explanation.

like image 423
CodeAngel Avatar asked Mar 17 '15 13:03

CodeAngel


People also ask

Is Java lambda a closure?

Java supports lambda expressions but not the Closures. A lambda expression is an anonymous function and can be defined as a parameter. The Closures are like code fragments or code blocks that can be used without being a method or a class.

Why a lambda expression forms a closure?

a function that can be treated as an object is just a delegate. What makes a lambda a closure is that it captures its outer variables. lambda expressions converted to expression trees also have closure semantics, interestingly enough.

What are closures in Java?

Closures are the inline-function valued expressions which means that they are the class functions with bounded variables. Closures can be passed to another function as a parameter. A closure gives us access to the outer function from an inner function.

What is the lambda keyword used for what is a closure?

In C++11 and later, a lambda expression—often called a lambda—is a convenient way of defining an anonymous function object (a closure) right at the location where it's invoked or passed as an argument to a function.


Video Answer


2 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).

like image 132
aioobe Avatar answered Oct 28 '22 20:10

aioobe


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
like image 40
uraimo Avatar answered Oct 28 '22 20:10

uraimo