Abstract
The core new functionality in Java 8 is the introduction of lambdas. However, most introductions will show a couple of examples of lambdas, and then they leave it to the developer to figure out the rest. If you are going to be developing modern Java programs, however, you are going to need to be well versed in how lambdas can be used. Throughout this chapter, we will dig into the particularities of the syntax and semantics of lambdas, including how to create more complex lambdas, how to work with method calls and lambdas, and what this all looks like under the hood. We will also look at some classic ways to manipulate lambdas, and how they are implemented within Java. By the end of this chapter, you should be fluent in how to create the lambdas that you want.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
This strange fact provides the opportunity for a rather annoying and pedantic yet useful interview question: “Please write a closure in valid Java 7 syntax.”
- 2.
We will address why the final variables are necessary in the section below named “Lambdas Should Always Be Threadsafe.”
- 3.
The upper limit of arguments to a lambda function is de facto unlimited. If you discover that upper bound in the course of normal development, you are doing something really wrong.
- 4.
This is one of the places where Java is decidedly not a functional language: function types are still communicated via interfaces, instead of being intrinsic within the language itself.
- 5.
Some people claim that vargargs in lambdas are unnecessary, and that attempting to use them is a sign that you are doing something wrong. If you want to see both the good and the bad of vararg lambdas, see JavaScript’s arguments object and the places where it is directly used.
- 6.
If you are the kind of person who knows OCaml, you are the kind of person who probably just caught me in a pedagogical lie. For the rest of you, discovering how I lied to you is left as an exercise to the reader.
- 7.
As we will see in Listing 2-15, the best way to confuse the compiler is with overloaded methods, especially if you combine overloaded methods and inheritance. Aside from that, the type inference in the Oracle SDK is actually quite good.
- 8.
If you are looking for Java to allow you to override operators like + and -, you are going to be sorely disappointed. It’s just not something that will make it through the Java Community Process as things stand. You may want to look at the Java-OO javac plugin, however: http://amelentev.github.io/java-oo/
- 9.
Namely, an explicit lambda versus a method reference. For information on method references, see the next section.
- 10.
See “Don’t Use Null” below.
- 11.
See the FunJava project’s Functions class: it provides a compose method that takes a Function and a Consumer and provides a Consumer.
- 12.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2015 Robert Fischer
About this chapter
Cite this chapter
Fischer, R. (2015). Understanding Lambdas in Java 8. In: Java Closures and Lambda. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4302-5999-2_2
Download citation
DOI: https://doi.org/10.1007/978-1-4302-5999-2_2
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4302-5998-5
Online ISBN: 978-1-4302-5999-2
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)