Java1.8 Features

Java 8 is the first version of Java that supports this programming paradigm. The new concepts introduced starting with java 8 and which make possible the functional programming in Java are:

  1. Lambda expressions

  2. Functional interfaces

  3. Optional class

  4. Stream API

Lambda expressions allow passing a function as an input parameter for another function, which was not possible earlier. In short, lambdas can replace anonymous classes, making the code readable and easy to understand.

A lambda expression consists of two parts separated by the “->” operator. The left side of the operator represents the list of input parameters, while the right side represents the function body.

Functional Interface is also known as Single Abstract Method Interfaces or SAM Interfaces. It is a new feature in Java, which helps to achieve functional programming approach

Example 1

@FunctionalInterface

interface sayable{

void say(String msg);

}

public class FunctionalInterfaceExample implements sayable{

public void say(String msg){

System.out.println(msg);

}

public static void main(String[] args) {

FunctionalInterfaceExample fie = new FunctionalInterfaceExample();

fie.say("Hello there");

}

}