Introducing Java 8 Lambda Expressions
Java 8 boasts a set of new functionality to improve the state of the Java programming language, and one of the most anticipated features falls under the category of lambda expressions—or, in their common vernacular, closures. Don't get too excited yet. Java 8 is still slated to be released in summer 2013, which is eight months from the time of this writing, but through an early-access link hosted under the OpenJDK project, you can take the Project Lambda prototype for a spin right now.
Lambda expressions, which are encapsulated in a Java Specification Request (JSR 335, in this case), are defined with the following features (summarized from the specification):
- Functional interfaces: Functional interfaces contain a single abstract method. Instead of defining an anonymous inner class just to implement this method, JSR 335 allows you to pass the implementation of that abstract method, in the form of a functional descriptor, to the method that requires an implementation of the interface. Finally, we can get away from having to define anonymous inner classes for everything when we just want to do something simple.
- Using lambda expressions as methods: A lambda expression is similar to a method in that it provides a formal parameter list and a body, which can be an expression or a block of code. In the example I demonstrate shortly, you'll see how to use lambda expressions as methods.
- Method references: Method references are a mechanism to refer to a method without invoking it. In other words, you can pass a method a reference to another method, and the first method will invoke the second (referred) method when ready. This makes implementing callbacks much easier.
- New rules for determining "target types": For some expressions, called poly expressions, the target type can be deduced, meaning that the same expression can return different types at different times. The return type will be converted automatically to the proper target type. This capability becomes important when building generic lambda expressions and passing them to methods to operate on specific data types.
Download and Installation
As I mentioned earlier, you can download the prototype for Project Lambda from the OpenJDK project. Follow the binary link and choose the file for your operating system. Decompress it somewhere on your local hard drive (it doesn't require an installation, which ensures that you won't disrupt your existing Java installation).
Now you have a choice: Configure your environment to use the JDK 8 prototype, or simply PATH to it from your command prompt. In short, you need to set JAVA_HOME to the directory where you decompressed the JDK and add its bin directory to your PATH. For example, on Windows:
set JAVA_HOME=C:\jdk8 set PATH=%JAVA_HOME%\bin;%PATH%
Mac and Linux:
export JAVA_HOME=/home/user/jdk8 export PATH=$JAVA_HOME/bin:$PATH
Now you can access the Java compiler (javac) and the Java Virtual Machine (java) from your command line.