JB Header
Java 8 Function Descriptors Explained
Definition - In Java 8 a Function Descriptor is a term used to describe the signature of the abstract method of a Functional Interface Click to Read tutorial on Java 8 Functional Interfaces. The signature of the abstract method of a Functional Interface is syntactically the same as the signature of the Lambda Expression Click to Read Java 8 Lambda Expressions tutorial. Hence, a Function Descriptor also describes the signature of a lambda. Examples of function descriptors To illustrate let us see a few examples of function descriptors - Example 1: Lets say we have a functional interface named FirstInterface. It's described as below - [su_box title="Example 1 - FirstInterface.java" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java]package com.javabrahman.java8; @FunctionalInterface public interface FirstInterface { //Single abstract method public void singleMethod(String param); }[/java][/su_box] For the above interface, named FirstInterface, the signature of the abstract method OR the function descriptor is (String) -> void Example 2: Functional Interface SecondInterface.java has a slight variation of the abstract method - [su_box title="Example 2 - SecondInterface.java" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java]package com.javabrahman.java8; @FunctionalInterface public interface SecondInterface { //Single abstract method public long computeSum(int num1, int num2); }[/java][/su_box] For SecondInterface the function descriptor is (int,int) -> long Example 3: Java's in-built java.lang.Runnable interface has a single public void run() method. The function descriptor for Runnable interface will be () -> void Example 4: Lets see an example of a generic type based in-built functional interface named Function<T, R> introduced in Java 8 - [su_box title="Example 4 - In-built java.util.function.Function<T, R>" style="soft" box_color="#fcba43" title_color="#00000" radius="4" Class="for-shortcodebox"][java]@FunctionalInterface public interface Function<T, R> { /** * Applies this function to the given argument. * @param t the function argument * @return the function result of type R */ R apply(T t);[/java][/su_box] The function descriptor for Function<T, R> will be T -> R [su_spacer size="10"]