The long debate about how to introduce lambda expressions (aka closures) into Java is approaching an important moment: the implementation of lambdas and virtual extension methods is now feature-complete, and is officially due to be shipped in JDK 8 in March 2014. The biggest changes in the language since Java 5—at least—are not far away now.
I’ve started thinking about the new features, partly so that Phil Wadler and I could consider a new edition of our book Java Generics and Collections. The material there, especially the material about collections, will need a lot of changes to keep it current when lambda expressions become widely used.
The new features are not all easy to understand at first, so this FAQ is intended to give you the benefit of my labour in learning about them. I hope you find it useful, whether you are already familiar with lambda expressions or encountering them for the first time. All comments and contributions are welcome. I’m already pleased to acknowledge:
- the continuing input from the Oracle Java Language and Tools team, especially Stuart Marks; their collaboration is helping to greatly improve the scope and accuracy of this document;
- Raoul-Gabriel Urma, with whom I’ve had many interesting discussions and who is making a very helpful contribution to the answers.
(Of course, all errors and omissions are my responsibility.) I will be happy to include and acknowledge your contribution too. But please be aware that anything on this website may end up as part of the material of a new edition of Java Generics and Collections.
What’s the Purpose of this FAQ?
The question-and-answer format is intended to serve two purposes;
- It’s a tutorial: if you’re starting from scratch in learning about the upcoming changes to Java, you should be able to start with the first question, “What is a lambda expression?” and get a tutorial introduction by following the “next” link at the top right of each page;
- It’s a reference: if you want an answer to a particular technical question, jump straight to it. Since the Oracle team are also reading these answers, it’s reasonable to expect that this will become an authoritative popular reference to what’s going to happen.
- As part of the reference material, there’s a resources page, where I will maintain up-to-date links with the various rapidly-changing useful resources: documentation, presentations, build and download resources, tool support and mailing lists.
The tutorial sequence is divided into topics, to be read in the sequence listed below and in the left-hand sidebar on every page, which also corresponds to the “next” and “previous” links at the top of each post. An exception is the topic of “Design Rationale”; some questions here can also be found in the tutorial sequence, others are free-standing.
What’s Your Question?
Let me know what you think of this resource, how it could be improved, and what questions you would like to see answered here. Ask the FAQ!
1: Fundamentals of Lambda Expressions
What is a lambda expression?
Why are lambda expressions being added to Java?
What is a functional interface?
What is the type of a lambda expression?
Are lambda expressions objects?
Where can lambda expressions be used?
What are the scoping rules for lambda expressions?
Can lambda expressions be used to define recursive functions?
Can lambda expressions use variables from their environment?
What are method references?
What is the syntax for instance method references?
What are constructor references?
2: Default Methods
Questions about Design Rationale
(Questions labelled “(t)” are also included in the tutorial sequence above)
Why are lambda expressions being added to Java? (t)
Are lambda expressions objects? (t)
Why the restriction on local variable capture?
Where is the Java Collections Framework going? (t)
Why are Stream operations not defined directly on Collection?
Why are lambda expressions so-called?
Lambdas and closures—what’s the difference?
Strictly speaking, a closure is a lambda expression paired with an
environment that binds each of its free variables to a value. In Java,
lambda expressions will be implemented by means of closures, so the
two terms have come to be used interchangeably in the community.