9

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.

BuZZ-dEE
  • 3,875
  • 7
  • 48
  • 76
CodeAngel
  • 577
  • 1
  • 8
  • 31
  • Try next time to use the search function http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda – SubOptimal Mar 17 '15 at 13:37

3 Answers3

18

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

aioobe
  • 383,660
  • 99
  • 774
  • 796
4

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
Community
  • 1
  • 1
Umberto Raimondi
  • 16,269
  • 7
  • 44
  • 54
-2

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, e.g. Groovy has closures

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