Dharmesh Singh
6 min readFeb 17, 2018

Lambda Expressions and Functional Interface In Java 8

Lambda (λ) Expression :

Lambda calculus is a big change in mathematical world which has been introduced in 1930. Because of benefits of Lambda calculus slowly this concepts started using in programming world. “LISP” is the first programming which uses Lambda Expression.

The Main Objective of λLambda Expression is to bring benefits of functional programming into java.

What is Lambda Expression (λ):

Lambda Expression is just an anonymous(nameless) function. That means the function which doesn’t have the name, return type and access modifiers.

Lambda Expression also known as anonymous functions or closures.

Example 1: Normal Java method

public void m1() {
System.out.println(“Hello Java”);
}

Above Normal Java method, you can write using Lambda Expression (λ) in Following way:

( ) -> {
sop(“Hello Java”);
}

or

( ) -> { sop(“Hello Java”); }

or

() -> sop(“Hello Java”);

Example 2 : Normal Java Method with argument

public void add(int a, int b) {
System.out.println(a + b);
}

Above Normal Java method you can write using Lambda Expression (λ) in Following way:

(int a, int b) -> sop(a+b);

note 1: If the type of the parameter can be decided by compiler automatically based on the context then we can remove types also.

note 2 : The above Lambda expression we can rewrite as (a,b) -> sop (a+b);

Example 3: Normal Java Method with return

public String m1(String str) {
return str;
}

Above Normal Java method, you can write using Lambda Expression (λ) in Following way:

(String str) -> return str;

or

(str) -> str;

Conclusions :

  1. A lambda expression can have zero or more number of parameters(arguments).
    Example:
    ( ) -> sop(“Hello Java”);
    (int a ) -> sop(a);
    (inta, int b) -> return a+b;
  2. Usually we can specify type of parameter.If the compiler expect the type based on the context then we can remove type.
    Example:
    (int a, int b) -> sop(a+b);
    (a,b) -> sop(a+b);
  3. If multiple parameters present then these parameters should be separated with comma(,).
  4. If zero number of parameters available then we have to use empty parameter [ like ()].
    Example:
    ( ) -> sop(“Hello Java”);
  5. If only one parameter is available and if the compiler can expect the type then we can remove the type and parenthesis also.

6. Similar to method body lambda expression body also can contain multiple statements.if more than one statements present then we have to enclose inside within curly braces. if one statement present then curly braces are optional.

Once we write lambda expression we can call that expression just like a method, for this functional interfaces are required.

Functional Interfaces:

if an interface contain only one abstract method, such type of interfaces are called functional interfaces and the method is called functional method or single abstract method(SAM).
Example:
1) Runnable : It contains only run() method
2) Comparable : It contains only compareTo() method
3) ActionListener : It contains only actionPerformed()
4) Callable : It contains only call()method

Inside functional interface in addition to single Abstract method(SAM) we can write any number of default and static methods.

In Java 8 , SunMicroSystem introduced @FunctionalInterface annotation to specify that the interface is FunctionalInterface.

InsideFunctionalInterface we can take only one abstract method,if we take more than one abstract method then , we will get compilation error.

Inside FunctionalInterface we have to take exactly only one abstract method.If we are not declaring that abstract method then compiler gives an error message.

FunctionalInterface with respect to Inheritance:
If an interface extends FunctionalInterface and child interface doesn’t contain any abstract method then child interface is also FunctionalInterface

In the child interface we can define exactly same parent interface abstract method.

In the child interface we can’t define any new abstract methods otherwise child interface won’t be FunctionalInterface and if we are trying to use @FunctionalInterface annotation then compiler gives an error message.

FunctionalInterface Vs Lambda Expressions:

Once we write Lambda expressions to invoke it’s functionality, then FunctionalInterface is required. We can use FunctionalInterface reference to refer Lambda Expression.Where ever FunctionalInterface concept is applicable there we can use Lambda Expressions

Example 1: Without Lambda Expression (λ):

Above code with Lambda Expression (λ):

Example 2: Without Lambda Expression (λ):

Above code with Lambda Expression (λ):

Anonymous inner classes vs Lambda Expressions

Wherever we are using anonymous inner classes there may be a chance of using Lambda expression to reduce length of the code and to resolve complexity.

Example with anonymous inner class:

Above Example with Lambda Expression (λ):

What are the advantages of Lambda expression?

  1. We can reduce length of the code so that readability of the code will be improved.
  2. We can resolve complexity of anonymous inner classes.
  3. We can provide Lambda expression in the place of object.
  4. We can pass lambda expression as argument to methods.

Note:

  1. Anonymous inner class can extend concrete class, can extend abstract class,can implement interface with any number of methods but
  2. Lambda expression can implement an interface with only single abstract
    method(FunctionalInterface).
  3. Hence if anonymous inner class implements functionalinterface in that particular case only we can replace with lambda expressions.hence wherever anonymous inner class concept is there,it may not possible to replace with Lambda expressions.
  4. Anonymous inner class! = Lambda Expression
  5. Inside anonymous inner class we can declare instance variables.
  6. Inside anonymous inner class “this” always refers current inner class object(anonymous inner class) but not related outer class object.

Example

  1. Inside lambda expression we can’t declare instance variables.
  2. Whatever the variables declare inside lambda expression are simply acts as local variables.
  3. Within lambda expression ‘this” keyword represents current outer class object reference (that is current enclosing class reference in which we declare lambda expression)

note:

  1. From lambda expression we can access enclosing class variables and enclosing method variables directly.
  2. The local variables referenced from lambda expression are implicitly final and hence we can’t perform re-assignment for those local variables otherwise we get compile time error

Differences between anonymous inner classes and Lambda expression: