Java: Lambda Expressions and Anonymous Classes

Are they the same?

Since the release of Java 8, the language now supports Functional Programming, and we can write things like this:

Lambda Expressions

So, what are Lambda Expressions? Lambda Expressions are functions that don’t need a name and can be defined right in place. They are also known as lambda functions.

We can recognize a Lambda Expression because it contains an arrow “->” that separates the arguments and the function body.

But, how does the compiler understands that? Well, it is because of the Functional Interfaces.

Functional Interfaces

A functional interface is just a Java Interface that has one and only one abstract method. It can be annotated with @FunctionalInterface, but that’s just for the reader, it has no implications at all.

When we write a Lambda Expression in our code, the compiler checks if a compatible functional interface exists, and then it infers how to treat the expression. You can read more about functional interfaces here.

Anonymous classes

Let’s make a short review about anonymous classes.

An anonymous class is an expression. It is created when we implement a class in place, like this:

As we can see, the SomeInterface interface is also a Functional Interface: it has only one abstract method.

An Anonymous Class can be writen as a Lambda Expression

As long as the class or interface that we want to implement has only one abstract method, the above code is equivalent to the next one:

Because of the functional interface, the compiler knows that the lambda expression is valid. There is nothing strange about that. As long as this Lambda Expression takes a String as parameter and returns nothing (void), it can be compatible with the SomeInterface that the compiler is expecting, so it just implements it as an Anonymous Class.

Coding this way, we reduce a lot of boilerplate code and the intent of the implementation is pretty obvious at first sight.

So Lambda expressions are just Anonymous Classes

There are no secrets when using a lambda expression. I’ve heard some people talking about “some functional programming engine” working behind the scenes (?), or saying things like “Lambda expressions performs terribly bad” and so on… but actually, they are just anonymous classes. I really don’t know where these people get that ideas from, but I swear I’ve heard them.

The key about Lambda Expressions is that they represents algorithms. Even if they are Anonymous Classes and can be referenced as simple Java Objects, they are still representing algorithms, and it allows us to think about our code in a more functional way.

But there is one more thing: Lambda Expressions let us read the code in a way that we couldn’t otherwise. Let’s see the next code:

We can read the first part of the code as:

Given a sequence from namesList:

1. Filter those names that starts with a vocal

2. Convert the filtered names to upper case.

3. For each converted name, print it.

While the second part of the code says something like:

Given a sequence from namesList:

1. Call the filter method and pass this Predicate as parameter.

2. Then call the map method and pass this Function as parameter.

3. At least, call the forEach method and pass this Consumer as parameter.

Can you see the difference? In the former case we didn’t talk about classes, objects, Predicates, Functions, Consumers or whatever. We can read the code and know immediatly what it does.

Conclusion

Lambda Expressions are most useful in two ways:

  1. They save us from writing a lot of unnecessary, messy boilerplate code.
  2. They help us to think in a more functional way, which, combined with the Stream API, it gives us a full aproach to the Functional Programming Paradigm.

There is nothing strange or weird with Lambda Expressions, they are as simple as an Anonymous Class is.

Before Java 8, the only way to achieve this sort of dynamic algorithm implementation at place was using anonymous classes, and it was very ugly and painful to code that way. With the “new” feature of Lambda Expressions, those days are over.

Once we remove all the boilerplate code, we can focus on the algorithms (or functions) we want to implement instead of instances or objects or classes, so our mind is no longer worried about how messy the code is, but how should the algorithms work.

Hope this article helps demystify some of the things that are said about Lambda Expressions.

Written by

Just another Software Engineer who loves programming, reading and teaching

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store