java 8 study
Uncategorized

Functional Interfaces and Lambda Expressions in Java 8

In this article, we are going to study about Functional Interfaces and Lambda Expressions which are launched in Java 8.What is Functional interfaces and Lambda expressions, you will get to know more about it with the simple programme.Let’s just started.

What is Functional Interface?

An interface with exactly one abstract method is called a functional interface. We can use @FunctionalInterface annotation to mark an interface as a functional interface. RunnableActionListenerComparable are some of the examples of functional interfaces.

@FunctionalInterface annotation is used to ensure that the functional interface can’t have more than one abstract method. In case more than one abstract methods are present, the compiler gives an ‘Unexpected @FunctionalInterface annotation’ message. However, it is not mandatory to use this annotation but it is a good practice to use it in case of production applications as it makes the debugging easier.

functional interference in java

The java.util.function package contains many built-in functional interfaces in Java 8.

Let’s see an example code for Functional Interface

@FunctionalInterface

interface Cube

{

int calculate(int c);

}

class Foo

{

public static void main(String args[])

{

int input = 3;

// lambda expression to define the calculate method

Cube cube = (int c)->c*c*c;

int answer = cube.calculate(input);

System.out.println(answer);

}

}

The output of the above code is 27

 

Let’s see an example of Runnable Interface

class Foo

{

public static void main(String args[])

{

// lambda expression to create the object

new Thread(()->

{System.out.println(“New thread started”);}).start();

}
}

We have used the terms of lambda expressions while using Functional Interfaces. Let’s see what they are.


What are Lambda Expressions?

They are anonymous methods (methods without names) used to implement a method defined by a functional interface.

Some features of lambda expressions are:

  • Anonymous: It is anonymous because it doesn’t have an explicit name like a method would normally have.
  • Function: It can be thought of as a function because a lambda isn’t associated with a particular class like a method is. But like a method, a lambda has a list of parameters, a body, a return type, and a possible list of exceptions that can be thrown.
  • Passed around: A lambda expression can be passed as argument to a method or stored in a variable.
  • Concise: We don’t need to write a lot of boilerplate like we do for anonymous classes.


The Arrow Operator

Lambda expressions introduce the new arrow operator -> into Java. It divides the lambda expressions in two parts:

(c) -> c*c*c

The left side specifies the parameters required by the expression, which could also be empty if no parameters are required.

If you go back to above program of finding the cube in which we used this same expression. You can think of it as c becomes triple times the original value i.e. the input becoming the 3 times of its value.

Based on number of input parameters we can write the syntax of lambda expression as

  • Zero parameter:

() -> System.out.println(“Zero parameter lambda expression”);

  • One parameter:

(n) -> System.out.println(“Input parameter is: ” + n);

It is not mandatory to use parentheses, if the type of that variable can be inferred from the context

  • Multiple parameters :

(p1, p2) -> System.out.println(“Multiple parameters: ” + p1 + “, ” + p2);

Let’s see some examples of lambda expressions via a simple program:

interface NumberTest {

boolean performTest(int input);

}

class Foo{

public static void main(String args[]) {

NumberTest isEven = (e) -> (e % 2) == 0;

NumberTest isNegative = (n) -> (n < 0);

// Output: false

System.out.println(isEven.performTest(7));

// Output: true

System.out.println(isNegative.performTest(-4));
}

}

Above program uses an interface named NumberTest which we can call a Functional Interface as there is only single abstract method in it.

The program checks for even and negative numbers using lambda expression and returns a Boolean value based on the computation performed.

Leave a Reply

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

Back To Top