There are lot of things happening in the Project Lambda branch of JDK 8 and these are quite significant for Java programmers. I know lot of Anti-Java people would criticize these changes claiming them to be too late. But for loads of the Java developers out there its a welcome change. At this point of time the specs for Project Lambda part of JSR 335 are evolving, but I think lot of basic ground work done is kind of frozen. For those who want to try out the Project Lambda you should download the binaries from here.
The important concepts as part of Project Lambda are:
- Virtual Extension Methods
- Functional Interfaces
- Lambda Expressions
- API Enhancements to Collections library
- Method and Constructor references
I have tried my hand at some of these concepts and barely managed to scratch the surface. I have blogged about few of the concepts with examples:
Virtual Extension Methods
We all know how limiting the interfaces can be, without being able to have method implementations in interfaces we at times tend to copy over the common implementations across different classes or create a adapter class with the common implementations. This is one side of it, but the other side is that it gives no scope for API enhancements without breaking the existing code. You cannot get away with adding a new method in the Interface without breaking millions of lines of code using your API. A similar kind of issue was being faced by the collections library which started to show their age. So a new feature in the language without the existing APIs being able to support them is hardly of any value. With an idea to enhance the existing collections API the concept of Virtual Extension Methods was introduced. In the early draft version 2 these have been referred to as default methods.
The blog posts which I wrote for this were:
- Introduction to Virtual Extension Methods in Java 8 .
- Resolution of invocation of Virtual Extension Methods.
- Use of Virtual Extension Methods in Java 8 API.
The Single Abstract Method (SAM) classes are now being referred to as Functional Interfaces. And these functional Interfaces can be replaced by lambda expression. The compiler then identifies the context in which this lambda expression is being used and then correspondingly converts the lambda expression into the SAM class. There’s another concept associated with this which is: “Functional Descriptor”. A functional descriptor of the Functional interface is the method type of the single abstract method of the interface.
Blog post related to this:
This is the main feature of this JSR. The idea of this is to support closures in Java. All those Anonymous Inner classes can be replaced by the Lambda expressions. The Collections API has also been enhanced with new APIs which accepts these lambda expressions.
The blog posts which I wrote for this are:
- A sneak peak at the Lambda expression in Java 8.
- Enhanced Collections API in Java 8 supports Lambda expressions.
- Using Lambda expressions of Java 8 in JavaFX event handlers.
With the above blog posts I have barely scratched the surface. There’s lot to be learnt in this and a sound knowledge in Generics can help to a really great extent. Lot of them would say that the specs are in Draft and things can change, but I believe its always good to know what was there before and what changed and the reason for change. And its always good to keep yourself one step ahead of others so that when it is released you are in quite a good shape to welcome the changes!
Please feel free to correct me if I have made any mistakes in my understanding of the various features. There’s always an opportunity to learn from mistakes.