Professional Documents
Culture Documents
03-Benefits of Lambda Expressions in Java Makes The Move To A Newer JDK Worthwhile
03-Benefits of Lambda Expressions in Java Makes The Move To A Newer JDK Worthwhile
03-Benefits of Lambda Expressions in Java Makes The Move To A Newer JDK Worthwhile
To get a full understanding of the benefits of lambda expressions in Java and how they provide conciseness
and readability, just look at how functional interfaces, that is, an interface that only defines a single method,
were implemented prior to lambda expressions. There were basically two approaches that could be used.
One approach was to define a separate class that implements the interface, while the second was to use an
anonymous inner class.
An anonymous inner class allows developers to define and implement a functional interface at the point in
the code where it is needed. In the prior example, we needed to create a separate class named
MyComparator. With anonymous inner classes, you don’t define a separate class, you simply implement the
methods requires, thus the name anonymous. Here’s how the Comparator example looks when
implemented using an anonymous inner class.
Anonymous inner classes are popular, and much preferred over creating separate implementing classes.
They also allow for the implementing code to be written at the spot in which the code is used, which makes
troubleshooting and long-term maintenance easier.
There are drawbacks to anonymous inner classes though. One major drawback is that the code does not
look pretty. If anything, it looks bloated. Furthermore, the ability to reuse this code is muted, as there’s not
way to invoke an anonymous inner class from outside of the code in which it is defined. And a more
innocuous issue with anonymous inner classes is the peculiar variable scope they introduce directly within a
class. Variables defined outside of an inner class do not employ the traditional rules pertaining to block scope
within a Java class. As a result, it is easy to fall prey to a bug-inducing anti-pattern known as variable
shadowing. This can create bugs that are very difficult to troubleshoot.
With lambda expressions, the goal is to minimize as many of the drawbacks to using a single class or
anonymous inner class when implementing a functional interface, while at the same time maximizing the
benefits. If you look at the Comparator example implemented using a lambda expression in Java, there’s no
debating the fact that the code is much more concise and compact.
This simple example of lambda expressions is only sufficient to demonstrate a few of the benefits of lambda
expressions in Java as compared to other approaches. As you explore lambda expressions at a more
advanced level, you will discover how they also make iterative processing easier using the new, functional
forEach method. And because lambda expressions can be assigned to a variable, they provide an opportunity
for reuse that simply isn’t possible with anonymous inner classes.
It took a long time for lambda expression to finally make it into the Java language, but given the benefits of
using lambda expressions in Java code, it’s no wonder that developers are excited when their target
deployment platform gets upgraded to a newer version of the JDK.