Skip to main content

Understanding Lambdas in Java 8

  • Chapter
  • First Online:
Java Closures and Lambda
  • 2416 Accesses

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

eBook
USD 16.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 16.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 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. 2.

    We will address why the final variables are necessary in the section below named “Lambdas Should Always Be Threadsafe.”

  3. 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. 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. 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. 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. 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. 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. 9.

    Namely, an explicit lambda versus a method reference. For information on method references, see the next section.

  10. 10.

    See “Don’t Use Null” below.

  11. 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. 12.

    See http://blog.enfranchisedmind.com/2007/09/use-vrs-reuse-or-the-second-derivitive-of-programming/ and http://blog.enfranchisedmind.com/2007/09/development-acceleration-the-second-derivative-of-functionality/ for more on this.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints 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

Publish with us

Policies and ethics