What are functional interfaces used for in Java 8?

why functional interface introduced in java 8
built-in functional interfaces in java
java functional interface tutorial
can functional interface have static methods
consumer functional interface
java 8 stream supplier example
predicate functional interface
java 8 features

I came across a new term in Java 8: "functional interface". I could only find one use of it while working with lambda expressions.

Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface.

For example:

@FunctionalInterface
interface MathOperation {
    int operation(int a, int b);
}

How useful it is in Java 8 other than just working with lambda expressions?

(The question here is different from the one I asked. It is asking why we need functional interfaces while working with lambda expressions. My question is: why other uses do functional interfaces have besides with lambda expressions?)

@FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.

But you can use lambdas without this annotation as well as you can override methods without @Override annotation.

From docs

a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere

This can be used in lambda expression:

public interface Foo {
  public void doSomething();
}

This cannot be used in lambda expression:

public interface Foo {
  public void doSomething();
  public void doSomethingElse();
}

But this will give compilation error:

@FunctionalInterface
public interface Foo {
  public void doSomething();
  public void doSomethingElse();
}

Invalid '@FunctionalInterface' annotation; Foo is not a functional interface

Java 8 Functional Interfaces, They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A  Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface.

The documentation makes indeed a difference between the purpose

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.

and the use case

Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.

whose wording does not preclude other use cases in general. Since the primary purpose is to indicate a functional interface, your actual question boils down to "Are there other use cases for functional interfaces other than lambda expressions and method/constructor references?"

Since functional interface is a Java language construct defined by the Java Language Specification, only that specification can answer that question:

JLS §9.8. Functional Interfaces:

In addition to the usual process of creating an interface instance by declaring and instantiating a class (§15.9), instances of functional interfaces can be created with method reference expressions and lambda expressions (§15.13, §15.27).

So the Java Language Specification doesn’t say otherwise, the only use case mentioned in that section is that of creating interface instances with method reference expressions and lambda expressions. (This includes constructor references as they are noted as one form of method reference expression in the specification).

So in one sentence, no, there is no other use case for it in Java 8.

Functional Interfaces in Java 8, Java 8 introduces an annotation i.e. @FunctionalInterface too, which can be used for compiler level errors when the interface you have annotated violates the  Legacy Functional Interfaces. Not all functional interfaces appeared in Java 8. Many interfaces from previous versions of Java conform to the constraints of a FunctionalInterface and can be used as lambdas. A prominent example is the Runnable and Callable interfaces that are used in concurrency APIs.

As others have said, a functional interface is an interface which exposes one method. It may have more than one method, but all of the others must have a default implementation. The reason it's called a "functional interface" is because it effectively acts as a function. Since you can pass interfaces as parameters, it means that functions are now "first-class citizens" like in functional programming languages. This has many benefits, and you'll see them quite a lot when using the Stream API. Of course, lambda expressions are the main obvious use for them.

Functional Interfaces In Java, Java functional interfaces are interfaces with a single abstract (unimplemented) method. The term Java functional interface was introduced in Java 8. The UnaryOperator interface can be used to represent an operation  How to Use Functional Interfaces in Java 8 A new concept introduced in Java 8, functional interfaces were added to support lambda expressions. Within this article, we’ll be covering what functional interfaces are and the reason why they were added to Java 8, as well as the benefits they provide.

Not at all. Lambda expressions are the one and only point of that annotation.

Java 8 - Functional Interfaces, @FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static  Instance Creation for Functional Interface. To create instances from your functional interfaces you have three ways: Create a class that implements your functional interface and just make an instance of that defined class. (Used Prior to Java 8). Create a method reference expression (Anonymous Instance, Used Prior to Java 8).

A lambda expression can be assigned to a functional interface type, but so can method references, and anonymous classes.

One nice thing about the specific functional interfaces in java.util.function is that they can be composed to create new functions (like Function.andThen and Function.compose, Predicate.and, etc.) due to the handy default methods they contain.

Java Functional Interfaces, Java functional interfaces: An interface with only single abstract method. We have been using functional interfaces even prior to java8, they were used by  The major benefit of java 8 functional interfaces is that we can use lambda expressions to instantiate them and avoid using bulky anonymous class implementation. Java 8 Collections API has been rewritten and new Stream API is introduced that uses a lot of functional interfaces.

What are functional interfaces used for in Java 8?, These interface with just one abstract method was used to pass around code, just like you pass a function in functional programming language  Functional Interfaces in Java 8 allows exactly one abstract method inside them. These interfaces are also called as Single Abstract Method interfaces (that is, SAM Interfaces). The instances of functional interfaces can be created with lambda expressions, method references, or with the constructor references.

Java 8 - Functional Interfaces, The interfaces in this package are general purpose functional interfaces used by the JDK, and are available to be used by user code as well. While they do not  Functional Interfaces In Java. A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods.

What is a Functional interface in Java 8 , Functional interfaces provide target types for lambda expressions and method references. Each functional interface has a single abstract method, called the functional method for that functional interface, to which the lambda expression's parameter and return types are matched or adapted.

Comments
  • It looks duplcate to this link. They also talk about why there should be only one method in Functional Interface. stackoverflow.com/questions/33010594/…
  • @KulbhushanSingh I saw this question before posting... Both questions sense difference...
  • To be more precise, you have to have exactly one abstract method that doesn’t override a method in java.lang.Object in a functional interface.
  • …and it’s slightly different to "not have more than one public method besides static and default"…
  • @lwpro2 a typo. Sure it was about obj -> obj.doSomething(anyObj). Thanks for noticing!
  • Still don't understand any point of having it. Why would anyone on earth bothers checking how many methods does his/her interface has. Marker interfaces still have a point and a specific purpose. The documentation and the answer only explain what it does, not how is it of any use at all. And "use" is exactly what the OP asked. So I wouldn't recommend this answer.
  • @VNT the compilation error get the clients of this interface, but not the interface itself can change. With this annotation the compilation error is on the interface, so you make sure that nobody will break the clients of your interface.
  • Might just be asking for a little too much or irrelevant(you can choose not to answer), but what would you suggest when someone created a utility public static String generateTaskId() versus to make it more "functional" someone else chose to write it as public class TaskIdSupplier implements Supplier<String> with the get method using the existing generation implementation. Is that a misuse of functional interfaces, especially reusing the Supplier from the JDK built-in? PS: I couldn't find a better place/Q&A to ask this. Happy to migrate if you could suggest.
  • @Naman you are not making the utility method more functional when you create a named class TaskIdSupplier. Now, the question is why you created the named class. There are scenarios where such a named type is needed, e.g. when you want to support finding the implementation via ServiceLoader. There is nothing wrong with letting it implement Supplier then. But when you don’t need it, don’t create it. When you only need a Supplier<String>, it’s already sufficient to use DeclaringClass::generateTaskId and eliminating the need for an explicit class is the point of this language feature.
  • To be honest, I was looking towards a justification for a recommendation that I was passing on. For some reason at work I didn't really felt that the TaskIdSupplier implementation was worth the effort, but then the concept of ServiceLoader totally skipped off my mind. Encountered a few questions during these discussions we were having such as What is the use of Supplier's public existence when one can go ahead and develop their own interfaces? and Why not have public static Supplier<String> TASK_ID_SUPPLIER = () ->... as a global constant?. (1/2)
  • @Naman the idiomatic way to represent functions in Java are methods and evaluating those functions is identical to invoking them. Never should a developer be forced to do variable.genericMethodName(args) instead of meaningfulMethodName(args). Using a class type to represent a function, whether via lambda expression/ method reference or a manually created class, is only a vehicle to pass the function around (in the absence of true function types in Java). This should only be done when needed.
  • When you have a small code fragment only being passed around, you may create a lambda expression encapsulating it. Whenever there’s also a need to invoke it like a method (this includes scenarios with a need for testing, when the code fragment is not trivial), create a named method that can be invoked and use a method reference or a lambda expression/ explicit class encapsulating a call, to pass it around when needed. Constants are only useful when you don’t trust the efficiency of lambda expressions or method references embedded in your code, in other words, they are almost never needed.
  • Well, lamdbas work without the annotation as well. It's an assertion just like @Override to let the compiler know that you intended to write something that was "functional" (and get an error if you slipped).