Java Reference
In-Depth Information
programmers to write more parallel programs, taking advantage of all the available cores in the processor. Java has
supported concurrent programming since the beginning. It added support for parallel programming in Java 7 through
the fork/join framework, which was not easy to use.
Functional programming, which is based on Lambda calculus, existed long before object-oriented programming.
It is based on the concept of functions, a block of code that accepts values, known as parameters, and the block of
code is executed to compute a result. A function represents a functionality or operation. Functions do not modify data,
including its input, thus producing no side-effects; for this reason, the order of the execution of functions does not
matter in functional programming. In functional programming, a higher order function is an anonymous function that
can be treated as a data object. That is, it can be stored in a variable and passed around from one context to another.
It might be invoked in a context that did not necessarily define it. Note that a higher order function is an anonymous
function, so the invoking context does not have to know its name. A closure is a higher order function packaged with
its defining environment. A closure carries with it the variables in scope when it was defined, and it can access those
variables even when it is invoked in a context other than the context in which those variables were defined.
In recent years, functional programming has become popular because of its suitability in concurrent, parallel,
and event-driven programming. Modern programming languages such as C#, Groovy, Python, and Scala support
functional programming. Java did not want to be left behind, and hence, it introduced lambda expressions to support
functional programming, which can be mixed with its already popular object-oriented features to develop robust,
concurrent, parallel programs. Java adopted the syntax for lambda expressions that is very similar to the syntax used
in other programming languages such as C# and Scala.
In object-oriented programming, a function is called a method and it is always part of a class. If you wanted
to pass functionality around in Java, you needed to create an object, add a method to the object to represent the
functionality, and pass the object around. A lambda expression in Java is like a higher-order function in functional
programming, which is an unnamed block of code representing a functionality that can be passed around like data.
A lambda expression may capture the variables in its defining scope and it may access those variables later in a context
that did not define the captured variable. This features let you use lambda expressions to implement closures in Java.
Java 8 introduced lambda expressions that represent an instance of a functional interface. You were able to do
everything prior to Java 8 using anonymous classes what you can do with lambda expressions. Functional interfaces
are not a new addition in Java 8; they have existed since the beginning.
So why and where do we need lambda expressions? Anonymous classes use a bulky syntax. Lambda expressions
use a very concise syntax to achieve the same result. Lambda expressions are not a complete replacement for
anonymous classes. You will still need to use anonymous classes in a few situations. Just to appreciate the conciseness
of the lambda expressions, compare the following two statements from the previous section that create an instance of
the StringToIntMapper interface; one uses an anonymous class, taking six lines of code, and another uses a lambda
expression, taking just one line of code:
// Using an anonymous class
StringToIntMapper mapper = new StringToIntMapper() {
@Override
public int map(String str) {
return str.length();
}
};
// Using a lambda expression
StringToIntMapper mapper = (String str) -> str.length();
Syntax for Lambda Expressions
A lambda expression describes an anonymous function. The general syntax for using lambda expressions is very
similar to declaring a method. The general syntax is
(<LambdaParametersList>) -> { <LambdaBody> }
 
Search WWH ::




Custom Search