Lambda expression is one of the new features of Java 8. It is mainly used to implement functional interfaces. This article covers 3 aspects:

  1. Traditional Approach (Anonymous Inner Class)
  2. New approach (Lambda Expression)
  3. Pros and cons of Lambda

1. Anonymous inner class

It is used for implementing interfaces or evoking the override methods of a class and/or an interface. it is declared and instanced at the same time. Just like creating an instance and defining local variables, it is created locally in the method, or at the class level. It does not have a name for itself so that’s why it is called an “Anonymous” inner class.

Image 1

In the above code, the snippet PerformCalulation is implemented when its interface is called. This is the anonymous inner class and its implementation overrides the calculate method.

Lambda Expression

It is used for implementing the functional interface.

What is a functional interface?

An Interface with only one abstract method is called a functional interface. A “@FunctionalInterfaceannotation is used to indicate a functional interface.

Image 2

In the above code, the snippet PerformCalculation interface is the functional interface. If you check Image 1 and Image 2, both will print 5. In image 2 we have used a Lambda Expression in lines 10 to 12.

Syntax of Lambda

(Parameter 1,Parameter 2)  ->  { method body }

Where:

  • Parameters 1 and 2 are argument lists which are passed on to the method.
  • The arrow mark is used to separate the argument list and the method body.
  • (the method body is the actual implementation of the method)

There are other syntaxes for lambdas and they depend upon the argument list as well as the method body:

 

Syntax Explanation
() -> true; // 0 parameters
a -> a.startsWith(“test”);   // 1 parameter
(String a) -> a.startsWith(“test”); // 1 parameter
(a, b) -> a.startsWith(“test”);      // 2 parameters
(String a, String b) -> a.startsWith(“test”); // 2 parameters
a, b -> a.startsWith(“test”); // DOES NOT COMPILE
a -> { a.startsWith(“test”); }; // DOES NOT COMPILE
a -> { return a.startsWith(“test”) }; // DOES NOT COMPILE

The sixth snippet is not compiled because if there is more than one argument it should be enclosed in a parentheses “().” Moreover, if you define an argument, it should be enclosed in parentheses too.

The seventh snippet is not compiled because it does not have a “return statement” – If you use braces “{}”  in the body than you should have a return statement in the body.

The eighth snippet will not compile because the line is missing a semicolon.

3. Pros and Cons of Lambda

Pros

  1. It has a simple, and easily readable syntax as compared to the anonymous inner class.
  2. It reduces the number of lines and boilerplate code in comparison to the anonymous inner class. In the code below, the highlighted blue syntax lines can be removed if we use Lambda.

3. When we compile the class containing the anonymous inner class, it will create one extra class file for the anonymous  class.

      Here the Lambda1$1.class file is created due to the anonymous class. However, if we  

      compile Lambda it will not create a class file.

Cons

We can only use it when we implement the functional interface.