Java Reference
In-Depth Information
Figure 1.4. Passing the method reference File::isHidden to the method
listFiles
Lambdas—anonymous functions
As well as allowing (named) methods to be first-class values, Java 8 allows a richer idea of
functions as values, including lambdas [ 4 ] (or anonymous functions). For example, you can now
write (int x) -> x + 1 to mean “the function that, when called with argument x, returns the value
x + 1.” You might wonder why this is necessary because you could define a method add1 inside a
class MyMathsUtils and then write MyMaths-Utils::add1! Yes, you could, but the new lambda
syntax is more concise for cases where you don't have a convenient method and class available.
Chapter 3 explores lambdas in detail. Programs using these concepts are said to be written in
functional-programming style—this phrase means “writing programs that pass functions around
as first-class values.”
4 Originally named after the Greek letter λ (lambda). Although the symbol isn't used in Java, its
name lives on.
 
Search WWH ::




Custom Search