There are a lot of new syntactic features being added to the upcoming JDK 8 and some of this cool “new” things like lambda expressions and extension methods could distract us and make us lose the perspective from the fact that the most important new feature in the JDK 8 is actually its support for parallelism.
As a matter of fact, I believe parallelism support is the only trendy new feature in this release of Java, and from all the “new” things being added, this one is the most important to evolve the language in the right direction. The other features, namely, lambda expressions and extension methods, have been added only to ease the writing of parallel APIs.
Lambda expressions exist since the beginnings of computing. The theory behind them, known as lambda calculus, was formulated by Alonso Church between 1930-1950, and his work constitutes the building block on which the functional programming paradigm rests.
So, this fantastic new feature is being added to Java 8 as well, but only with the purpose of making the writing of parallel APIs simpler, and not because everyone else has it. In terms of functionality, there is nothing in Java that we could do using lambda expressions that we could not do without them. However lambda expressions do make the code more succinct and therefore the APIs developed with them are cleaner. Code written with them tends to be more brief due to their simplified syntax and in Java they eliminate the need for boilerplate code in many scenarios. Also closures can be more easily defined through lambda expressions and these are particularly useful when writing parallel APIs because they make extensive use of high-order programming, and this is the field in which lambda expressions really shine.
Now, in order to add support for lambda expressions to a language like Java, many of the existing APIs, particularly the collections APIs, would need to suffer changes. This makes the evolution of the language more complicated, above all if, as it has been customary in Java releases, we intend to keep backwards compatibility.
This problem becomes more evident when it comes to the evolution of interfaces: once an interface has been publicly released it is impossible to add new things to it without breaking its existing clients. We cannot define new methods without making the clients break because the compiler will require that all the new methods of the interface are implemented. So the big question here is, how can we evolve the existing APIs to support lambda expressions without breaking current clients?
Well, the answer to this question has been the addition of extension methods to the language. And yet again, there is no surprise in this, other languages have supported similar features (i.e. mixins in languages like Ruby, Scala , and extension methods in C#). In Java these extension methods have been defined as a new feature to support implementation of behavior in interfaces. This is a great and logical addition to the language, and one that will certainly simplify the evolution of existing APIs, not only those of the JDK, but also those created by everyone else. And in this case too, we have to recognize that the addition of this new feature has been highly motivated, at the end, by the need to support parallelism.
In summary, we have extension methods because we needed them to add API support for lambda expressions, and we need lambda expressions to make the writing of parallel APIs simpler and cleaner. So, clearly, the feature we should really care about here is parallelism, since it appears to be the source of all good bits in this new release of Java.
The actual trend here is that chip manufactures have hit against the wall of Moore’s law, and have decided, at least for the moment, that the way to increase computational power is by means of multicore architectures, that is by adding more processors to a computer instead of adding faster clocks. This represents an important paradigm shift in the way we write software. The free ride is over, now the new microchips will not make our existing software faster and it is us, the software developers, who are responsible to make the best use of these multicores by means of harnessing the power of parallelism, that is, by dividing the tasks our software carries out in such a way that we can keep all these cores as busy as possible, and without interrupting each other.
Evidently, writing programs and APIs for this new model of computation requires new tools. In Java, this has started with the addition of the fork/join framework in Java 7 and now continues with the addition of parallel streams and lambda expressions in the upcoming release of Java 8.
So, let’s not permit that this cool “new” syntactic features in Java distract our attention of what really matters: the new features for parallelism.
- Closures for Java
- Closures for Java: Q&A
- The Free Lunch is Over
- Understanding the Closures Debate
- Why are lambda expressions being added to Java?
- It is time to get good at Functional Programming