Java: Lambda Expressions and Anonymous Classes

Marcelo Valls
3 min readSep 19, 2020

--

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 written 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. As long as this Lambda Expression takes a String as a 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

I’ve once heard a dev talking about “some functional programming engine” working behind the scenes (?), or saying things like “Lambda expressions perform terribly bad” and so on… but actually, they are just anonymous classes implemented in the place.

So what are the differences between Lambda Expressions and Anonymous Classes?

The key about Lambda Expressions is that they represent 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 immediately 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 in situ 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.

Thanks for reading!

--

--

Marcelo Valls
Marcelo Valls

Written by Marcelo Valls

Just another Software Engineer who loves programming, reading and teaching

No responses yet