Introduction
Lambda expressions are a new and important feature included in Java Platform Standard Edition 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection. In addition, new concurrency features improve performance in multicore environments.
JSR 335 - Project Lambda
Lambda Expressions are part of Java Specification Request (JSR 335) which is lead by Brian Goetz. JSR 335 is a coordinated co-evolution of the Java platform. It is going to change the language, libraries and VM.
When Java was first introduced in 1995 not many languages had closures, but they are present in pretty much every major language today, even C++.
In 1997 Java 1.1 added inner classes - a weak form of closures, which has many limitations as it is too bulky and the rule of name resolutions are too complex.
In 2006-2008, a vigorous community debate started about closures and multiple proposal arose including BGGA and CICE. Each had a different principle:
Currently the EDR (Early Draft Review) completed, prototype (source and binaries) available in OpenJDK and it is going to be a part of Java SE 8.
Why Lambda?
Java is the lone holdout among mainstream OO languages at this point to not have closures, and Lambda is no longer a radical idea, it was 10 years ago, times change and it is not anymore.
It provides libraries a path to multicore. Parallel-friendly APIs needs to have internal iteration and internal iteration needs a concise code-as-data mechanism.
It empower library developers by providing more powerful and flexible libraries and by providing higher degree of cooperation between libraries and client code which makes everybody's life better.
What is Lambda Expression?
A Lambda Expression is simply an anonymous method. Like a method it has an argument list, parameters, return type and has a set of thrown exception and a body. It has all the things that a method has except a name and it is not a member of a class, rather it is just an free floating expression.
Example:
It also can refer to values from the enclosing lexical scope:
More to come...
Lambda expressions are a new and important feature included in Java Platform Standard Edition 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection. In addition, new concurrency features improve performance in multicore environments.
JSR 335 - Project Lambda
Lambda Expressions are part of Java Specification Request (JSR 335) which is lead by Brian Goetz. JSR 335 is a coordinated co-evolution of the Java platform. It is going to change the language, libraries and VM.
- Language - lambda expressions (closures), interface evolution, better type inference.
- Libraries - Bulk parallel operations on collections.
- VM - Support for default methods and lambda conversion.
- More parallel-friendly.
- Enable delivery of more powerful libraries.
- Enable developers to write more concise, less error-prone code.
When Java was first introduced in 1995 not many languages had closures, but they are present in pretty much every major language today, even C++.
In 1997 Java 1.1 added inner classes - a weak form of closures, which has many limitations as it is too bulky and the rule of name resolutions are too complex.
In 2006-2008, a vigorous community debate started about closures and multiple proposal arose including BGGA and CICE. Each had a different principle:
- BGGA(Bracha Gafter Gosling Ahé) - Creating control abstraction in libraries and full closure support by Gilad Bracha, Neal Gafter, James Gosling and Peter von der Ahé.
- CICE (Concise Instance Creation Expressions) - Simplified inner classes to reduce syntactic overhead by Bob Lee, Doug Lea and Josh Bloch.
Currently the EDR (Early Draft Review) completed, prototype (source and binaries) available in OpenJDK and it is going to be a part of Java SE 8.
Why Lambda?
Java is the lone holdout among mainstream OO languages at this point to not have closures, and Lambda is no longer a radical idea, it was 10 years ago, times change and it is not anymore.
It provides libraries a path to multicore. Parallel-friendly APIs needs to have internal iteration and internal iteration needs a concise code-as-data mechanism.
It empower library developers by providing more powerful and flexible libraries and by providing higher degree of cooperation between libraries and client code which makes everybody's life better.
What is Lambda Expression?
A Lambda Expression is simply an anonymous method. Like a method it has an argument list, parameters, return type and has a set of thrown exception and a body. It has all the things that a method has except a name and it is not a member of a class, rather it is just an free floating expression.
| Argument List | Arrow Token | Body |
|---|---|---|
| (int x, int y) | -> | x + y |
Example:
(Object obj) -> obj.toString()In this example it takes an Object as an argument, and in its body it calls Object.toString() and returns that. By this it removes a lots of syntactical overhead, there is no return statement, no braces etcetera.
It also can refer to values from the enclosing lexical scope:
(Shape shape) -> shape.getColor().equals(color)Compiler can also infer parameter types from context, so in that case following statement is completely valid:
shape -> shape.getColor().equals(color)There is also a feature added to the Lambda Expression is called Method References, which behave just like a Lambda Expression. If you already have a method and if you want to use that method behaviour as Lambda Expression, you can directly use that Method Reference as:
Object::toStringThis is identical to the first example.
More to come...