What are closures?

Closures, or Lambda expressions, are anonymous functions. Like anonymous classes, which have been around for a while, lambda expressions can be passed into other functions as a type of a callback. This essentially evolves the callback or listener pattern used for years in Java. You would previously use inner classes (anonymous classes) but now, closures (anonymous functions) could be used for the same purpose.

Lambda expressions are anonymous functions, aimed at addressing the “vertical problem” by replacing the machinery of anonymous inner classes with a simpler mechanism. — link

I like this idea and I love to see languages grow. I always have reservations to adopt new coding syntax in an existing language in fear that it looks strange or is hard to read until it becomes commonly used.

Closures were left out of Java initially more because of time pressures than anything else. In the early days of Java the lack of closures was pretty painful, and so inner classes were born: an uncomfortable compromise that attempted to avoid a number of hard issues. But as is normal in so many design issues, the simplifications didn’t really solve any problems, they just moved them. —link

Some good references to check out about closures and Java:

Understanding the closures deate – link
JSR 335 – Lambda Expressions for the JavaTM Programming Language
State of the Lambda – Open JDK


2 thoughts on “What are closures?

  1. I wouldn’t say they are two different things but I get Milton’s point about all lambda expressions not being closures. It seems the Java community has made them one in the same and maybe that is because of the implementation.

Leave a Reply to Bob Balfe Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.