Java 8

  1. Java 8 Complete Tutorial In 3 Hour With Realtime Example | JavaTechie
  2. What's New In JDK 8

An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class.

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

Step 1) Go to link. Click on JDK Download for Java JDK 8 download. Step 2) Next, Accept License Agreement. Download Java 8 JDK for your version 32 bit or JDK 8 download for windows 10 64 bit. Step 3) When you click on the Installation link the popup will be open. Click on I reviewed and accept the Oracle Technology Network License Agreement for. To tackle this issue, Java 8 has the introduction of default methods. It might sound surprising but interface can have implementation code as well. You can declare default method in the interface and it won’t force client to implement default method. For example: reversed is default method in Comparator interface.

Test it Now

Output:

A functional interface can have methods of object class. See in the following example.

Example 2

Test it Now

Output:

Invalid Functional Interface

Download

A functional interface can extends another interface only when it does not have any abstract method.

Output:

Linux

Example 3

In the following example, a functional interface is extending to a non-functional interface.

Test it NowJava 8 update 251

Output:

Java 8 downloadQuestions

Java Predefined-Functional Interfaces

Java provides predefined functional interfaces to deal with functional programming by using lambda and method references.

You can also define your own custom functional interface. Following is the list of functional interface which are placed in java.util.function package.

InterfaceDescription
BiConsumer<T,U>It represents an operation that accepts two input arguments and returns no result.
Consumer<T>It represents an operation that accepts a single argument and returns no result.
Function<T,R>It represents a function that accepts one argument and returns a result.
Predicate<T>It represents a predicate (boolean-valued function) of one argument.
BiFunction<T,U,R>It represents a function that accepts two arguments and returns a a result.
BinaryOperator<T>It represents an operation upon two operands of the same data type. It returns a result of the same type as the operands.
BiPredicate<T,U>It represents a predicate (boolean-valued function) of two arguments.
BooleanSupplierIt represents a supplier of boolean-valued results.
DoubleBinaryOperatorIt represents an operation upon two double type operands and returns a double type value.
DoubleConsumerIt represents an operation that accepts a single double type argument and returns no result.
DoubleFunction<R>It represents a function that accepts a double type argument and produces a result.
DoublePredicateIt represents a predicate (boolean-valued function) of one double type argument.
DoubleSupplierIt represents a supplier of double type results.
DoubleToIntFunctionIt represents a function that accepts a double type argument and produces an int type result.
DoubleToLongFunctionIt represents a function that accepts a double type argument and produces a long type result.
DoubleUnaryOperatorIt represents an operation on a single double type operand that produces a double type result.
IntBinaryOperatorIt represents an operation upon two int type operands and returns an int type result.
IntConsumerIt represents an operation that accepts a single integer argument and returns no result.
IntFunction<R>It represents a function that accepts an integer argument and returns a result.
IntPredicateIt represents a predicate (boolean-valued function) of one integer argument.
IntSupplierIt represents a supplier of integer type.
IntToDoubleFunctionIt represents a function that accepts an integer argument and returns a double.
IntToLongFunctionIt represents a function that accepts an integer argument and returns a long.
IntUnaryOperatorIt represents an operation on a single integer operand that produces an integer result.
LongBinaryOperatorIt represents an operation upon two long type operands and returns a long type result.
LongConsumerIt represents an operation that accepts a single long type argument and returns no result.
LongFunction<R>It represents a function that accepts a long type argument and returns a result.
LongPredicateIt represents a predicate (boolean-valued function) of one long type argument.
LongSupplierIt represents a supplier of long type results.
LongToDoubleFunctionIt represents a function that accepts a long type argument and returns a result of double type.
LongToIntFunctionIt represents a function that accepts a long type argument and returns an integer result.
LongUnaryOperatorIt represents an operation on a single long type operand that returns a long type result.
ObjDoubleConsumer<T>It represents an operation that accepts an object and a double argument, and returns no result.
ObjIntConsumer<T>It represents an operation that accepts an object and an integer argument. It does not return result.
ObjLongConsumer<T>It represents an operation that accepts an object and a long argument, it returns no result.
Supplier<T>It represents a supplier of results.
ToDoubleBiFunction<T,U>It represents a function that accepts two arguments and produces a double type result.
ToDoubleFunction<T>It represents a function that returns a double type result.
ToIntBiFunction<T,U>It represents a function that accepts two arguments and returns an integer.
ToIntFunction<T>It represents a function that returns an integer.
ToLongBiFunction<T,U>It represents a function that accepts two arguments and returns a result of long type.
ToLongFunction<T>It represents a function that returns a result of long type.
UnaryOperator<T>It represents an operation on a single operand that returnsa a result of the same type as its operand.

Java 8 Complete Tutorial In 3 Hour With Realtime Example | JavaTechie

Next TopicJava 8 Stream

What's New In JDK 8