Home > Software Development > Java 8 New Features: Lambda Expression

Java 8 New Features: Lambda Expression

March 23rd, 2014 Leave a comment Go to comments

On last Tuesday (March 18, 2014), Oracle released the Java SE 8. Here is the official documentation from the company on the new features in Java SE 8, ranging from language itself, libraries, tool, and run-time.

After Java 6 was released in December 11, 2006, Oracle acquired Java through buying Sun Microsystem in 2010. There was inevitably disruption on the releases (more on Java release history here). Although Java 7 was released on July 28, 2011, I didn’t see wide adoption of the release. For one thing, the Java 6 and its update releases were good and stable enough for most applications, there has been no immediate need to upgrade to Java 7. With Java 8 release, I think quite some people will jump directly from 6 to 8. Even that is the case, don’t forget to check out what is new in Java 7.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure


One of the highly visible features in Java 8 is the lambda support. It has been a long debate whether the lambda should be added to the Java language. On one hand, what the lambda feature delivers can also be done in previous versions of Java language with interfaces. In theory, there is no need for the lambda. On the other hand, lambda feature can really shorten the verbose language itself – one line of lambda can do the same thing as a few line of anonymous code with interfaces. Overall, it’s a nice to have feature, not a must to have one. Then, the question becomes whether it’s worthwhile to shorten app code at the cost of complicating the language itself. With Java 8 release, the answer is clear.

A Quick Sample

The lambda feature is probably not as complicated as the generics that was added in Java 5. To explain what it is, it’s best to use a simple sample. Of course, you want to download the Java 8 GA release first at here. You can choose either JDK only or the bundle with Netbeans 8. With this release, Oracle made a smart decision to align its Netbeans IDE version with JDK vesion. If you just want to quickly try out Java 8, I think it’s good to use the bundle; otherwise, I would suggest to download the JDK and IDE separately because you will get more flexibility on choosing what edition of Netbeans or other IDEs. For me, because I had Netbeans 7.4 (it supports Java 8 beta) installed already, I just use it as it is and added the JDK 8 as a new platform. If you prefer to have Eclipse or IntelliJ, that is OK too. These IDEs had support Java 8 when it was still in beta.

With IDE configured, we can take a look at the following sample code that was inspired by one of the samples in Oracle’s tutorial.

package org.doublecloud.jave8demo;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class LambdaForGUI 
  public static void main(String[] args) 
    JButton button = new JButton("Java 8 Lambda Button");
    button.addActionListener(new ActionListener()
        @Override public void actionPerformed(ActionEvent e)
          System.out.println("@ Old Listener");
    button.addActionListener(e -> System.out.println("@ Lambda Listner"));
    JFrame frame = new JFrame("DoubleCloud Demo of Lamba Listener");
    frame.add(button, BorderLayout.CENTER);
    frame.setSize(400, 200);

As you can see from the code, there are two listeners that are registered with the JButton object. They can achieve pretty much the same thing – once button is clicked, each prints out a message at console. What is different is that the second one uses lambda, and is much shorter and therefore looks more concise than the first one. If you have lots of GUI code with listeners, it definitely make the code much easier to read and maintain.

More Generic Use Cases

The lambda feature is not only for GUI though. It can work with any interface with one method (or, function) defined. That is why the lambda line does not include any method name, but the parameter names (note here, not even parameter types) of the method so that you can refer to them in the body of method implementation. With the lambda expression, the code is cut down to the absolutely necessary only.

Here I make up another sample to show what I just talked about.

package org.doublecloud.jave8demo.lambda;
public class SimpleLambda
  public static void main(String[] args)
    Addable adder1 = new Addable()
      public int add(int a, int b)
        return a + b;
    Addable adder2 = (a, b) -> { return a + b; };
interface Addable
  public int add(int a, int b);

As you can see, you can use lambda with your own interface.

How to Avoid Writing Your Own Interfaces

The reason for the Addable interface to be in above sample is that there was no easy way to define a function before Java 8. It’s an overkill to write an interface while the real intention is to define a function signature that consists of a number of inputs and one result. Although the types of inputs and result can vary from method to method, they can be abstracted using generics. The only change left is really the number of inputs. Therefore it’s easy to pre-define a few commonly used function classes, say no parameter, one parameter, two parameters.

With these in mind, you can easily understand the *Function classes in the java.util.function package. To show how it works, let’s see a simplified version of the above sample, in which the Addable interface is removed. For that, we need a two parameter Function which is called BiFunction in the java.util.function package.

package org.doublecloud.jave8demo.lambda;
import java.util.function.BiFunction;
public class FunctionDemo
  public static void main(String[] args)
    BiFunction<Integer, Integer, Integer> adder = (a, b) -> a + b;

As you can see, there is no need to define any interface for your own function. You can use the *Function class to define your own logic.

The above analysis about the *Function classes is a bit too simplified because we didn’t consider the primitive types such as int, long, double, etc. Although they can boxed by their corresponding types like Integer, Long, Double, it huts performance. To better handle these cases, there are more classes designed, such as DoubleToIntFunction that “represents a function that accepts a double-valued argument and produces an int-valued result.” There could be many combinations for all of them to have their own classes. A compromise is to take the middle ground, for example the IntFunction class that “represents a function that accepts an int-valued argument and produces a result of type R.” The input parameter is set to be int, but the output can be anything including boxed int, long, etc.

Here is a code snippet illustrating the usage of the IntFunction class:

    IntFunction<Integer> sq = i -> i*i;
    System.out.println("Perfect square of 5 is " + sq.apply(5));

While you can define a function without interface, you no longer have the flexibility on the method name. All of the functions you define, you have to use the method name “apply”.


I think the new addition of Lambda feature in Java 8 is a positive thing even though it may take a bit time for developers to understand it, and even more time to use it effectively. As any other advance features, my recommendation is to use lambda where it is necessary, not where it simply makes your resume look better. For the later case, you can try other ways like blogging. :)

For more information on the Java 8 and lambda feature:

Java 8 documenation home
Java SE 8: Lambda Quick Start

  1. December 14th, 2017 at 22:51 | #1

    helpful and informative article!
    Thanks for giving insight into new features of java8, its good to know that java8 has come up with the new features that takes java into the ring where it belongs to. lambda expressions it is the most prominent function of java8. Thanks for this useful article!

  2. January 22nd, 2018 at 01:15 | #2

    A lambda expression is like a method, it provides a list of formal parameters and a body expressed in terms of those parameters. Many thanks for sharing this.

  1. March 26th, 2014 at 00:02 | #1
  2. March 31st, 2014 at 00:33 | #2
  3. April 7th, 2014 at 00:25 | #3