Introduction to lambda expressions

In this article I will give you a quick introduction about one of Java 8’s new features: lambda expressions. I will show you what lambda expressions are and how can you utilize this new feature.

What are lambda expression?

Lambda expressions are something like closures known from other programming languages — or anonymous functions or inner classes known from Java itself.

Lambda expressions want to leverage the usage of anonymous classes. You may remember how it is to register an action listener to a button in a graphical user interface:

Yes, this code above shows you an anonymous inner class which is only required to implement the only method actionPerformed(ActionEvent) of the ActionListener interface. And this is clumsy — even if modern IDEs can help you with code completion to fill in the body of the method.

However with lambda expressions you can implement this method simply like this:

This is much more better, isn’t it? Now it is time to take a deeper look at how you can construct your own lambda expression.

Lambda expressions have the following syntax:

The key to lambda expression is the -> arrow symbol.

Let’s look at a basic example:

There are some other characteristics which make the usage of lambda expression easier:

  • parentheses are optional around the parameters if only one parameter is required
  • type declaration of the parameters are optional
  • return statement is optional
  • curly braces around the expression body are optional — if the expression body contains a single statement

Because our simple expression qualifies for all four points mentioned above we can rewrite this lambda expression like this:

This makes things more readable.

Functional interfaces

Lambda expressions can be used to implement methods of so called functional interfaces. Newer interfaces can have the annotation @FunctionalInterface to indicate that this interface is a functional one — however every interface qualifies as a functional interface which has only one abstract method. That’s why ActionListener’s addActionListener can be replaces / implemented with a lambda expression even if the interface is not informally marked as a FunctionalInterface.

default methods are not counted as abstract because they have an implementation. If the interface overrides methods from java.lang.Object without providing an implementation this does not count as abstract either because the implementation will inherit the implementation from the java.lang.Object class. Let’s see an example:

The interface above is a functional interface even if we do not add the annotation. Now let’s create a lambda and use it. Remember, our lambda has to implement only the getName() function which does not need any parameters and returns a String:

If we run the example we get a result similar to this:

As you can see, the default method works fine with the lambda implemented and the toString is called from java.lang.Object because a direct implementation in the lambda expression is not present.

Some more examples

After we know lambda expressions, let’s see some examples where we can use them to improve our existing or new code.

Because lambda expressions are anonymous functions or inner classes we can replace calls to existing anonymous functions with them. Just like a GUI application’s action listeners or local implementations of the Runnable interface.

The example above implements the Runnable interface with an anonymous class. Now for this simple “Hello lambdas?” output this is quite some work to do.

The code example above uses lambda expressions and does exactly the same thing: it prints a string “Hello lambdas!” to the console — however without the fuzz of creating an anonymous class to implement the Runnable interface. Naturally, this code works only with functional interfaces (interfaces, which have only one method).

And as you can see, parameterless lambdas can be created too. For this you need to provide empty parentheses as the parameters to indicate that there is nothing.

Beside this, lambda expressions are closures too. This means, they are functions you can save in a variable and re-use them later. If we take a look at our example from the previous section, we can save it into a variable called square and re-use it later:

To use it, we can call it either directly or use it in some stream expression, like map:

The result of the code snippet above would be this:


Currying is the name of partial function application. This means, you have a function which takes more than one parameters but most of the parameters are known already and will stay the same — just one will change. And instead of providing every parameter when you call the function you can curry the function partially and set some default parameters.

Let’s take a look at the following example:

This code creates a simple Function which multiplies two variables of type Integer together. Because the returned function requires two arguments (x and y) we can create curried versions of these functions and use them later:

Now we have applied (curried) one variable to each of the functions. If we call these functions (apply the second variable) we can see that the results represent the values already applied:

Naturally calling apply() every time is a bit overkill but Java cannot make it currently better. It would be a better syntax if you could write following:

This does not work currently but it would be a nice syntactic sugar to add to the Java language.


Lambda expressions are powerful if you want to implement functional interfaces without the much ado of creating and instantiating a class implementing the interface — mostly because you only need it once (for example as an ActionListener for a button).

We looked at currying / partial function application and we discussed that some features would it make easier to use in Java and hopefully in a future release we can work with it.

Leave A Comment

Your email address will not be published. Required fields are marked *