@FunctionalInterface Annotation

Description

@FunctionalInterface annotation is defined in the java.lang package. We can optionally use it to mark a functional interface.

If the annotation @FunctionalInterface is annotated on a non-functional interface or other types such as classes, a compile-time error occurs.

An interface with one abstract method is still a functional interface even we don't annotated it with @FunctionalInterface.

Example


public class Main {
  public static void main(String[] argv) {
    Processor stringProcessor = (String str) -> str.length();
    String name = "Java Lambda";
    int length = stringProcessor.getStringLength(name);
    System.out.println(length);//from   ww w  . ja  v  a2s  . c  o  m

  }
}

@FunctionalInterface
interface Processor {
  int getStringLength(String str);
}

The code above generates the following result.





















Home »
  Java Lambda »
    Java Lambda Tutorial »




Java Lambda Tutorial