File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Features new in Java 8 and the fly likes Difference between a Lambda and a Closure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "Difference between a Lambda and a Closure" Watch "Difference between a Lambda and a Closure" New topic
Author

Difference between a Lambda and a Closure

Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1003
    
  47

Hi Richard,

First up, an apology: I missed your talk at Devoxx yesterday as I was next door listening to John Smart talk about testing.

And now for the question: What is the difference between a Lambda and a Closure?

I thought I knew the answer to this but listening to Peter Ledbrook this afternoon talk about Groovy, he threw up some code on the screen, at which I though "oh yes, that's a Lambda expression", only for him to say "but this isn't a Lambda, it's a Closure". Now I'm a little confused over the distinction.

Thanks
Tim


Tim Driven Development
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
The talk yesterday was good.
Those of us who went to Devoxx have an opportunity to revisit it because there will be repeats of the talks available, so you can make up on the talk later.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

A closure is a construct in which a function (possibly a lambda) is combined with variables that are not within local scope.

For instance:

This method returns a function that adds x to y. The function itself (in this case a lambda) references a variable that's not within its local scope (x). When you call the plus() method, that's when you create a closure that binds a value to x within the lambda.

y -> x+y is a lambda.
plus(3) creates a closure.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3646
    
  16

I just thought of a further clarification. plus(3) creates a closure that you can imagine looking like this: y -> 3+y.

The difference between that, and actually writing down the lambda y -> 3+y is that the lambda resides in the source code, while the closure is created at runtime and resides in memory.
Sean Corfield
Ranch Hand

Joined: Feb 09, 2011
Posts: 260
    
    5

An explanation I read the other day which I think helps is:
  • A lambda is a compile-time - syntax - construction.
  • A closure is a run-time - dynamic - construction.

  • A lot of people use the two terms interchangeably and others draw an incorrect distinction between them - which causes a lot of confusion.
    Tim Cooke
    Bartender

    Joined: Mar 28, 2008
    Posts: 1003
        
      47

    Some good responses, thanks very much. This might take me a little more processing and re-reading of these answers before I 'get it' but I think it's starting to make some sense now.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Difference between a Lambda and a Closure