Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The target type of this expression must be a functional interface in MethodReferences

Tags:

Why does the following code not compile.

Consumer con = (s) -> System.out::println;

It says

The target type of this expression must be a functional interface

even though Consumer is a Functional Interface. The below works just fine.

Consumer con2 = (s) -> {System.out.println(s);};

like image 512
HariJustForFun Avatar asked Feb 23 '17 06:02

HariJustForFun


People also ask

What is the target type of a lambda expression?

The parentheses for specifying the parameter can be omitted for a single-parameter lambda expression. The target type of the lambda expression—the functional interface ActionListener —is inferred from the context, which is a method invocation.

Is system out a functional interface?

The type of System. out. println is not a functional interface, since System.

Is lambda expression only for functional interface?

No, all the lambda expressions in this code implement the BiFunction<Integer, Integer, Integer> function interface. The body of the lambda expressions is allowed to call methods of the MathOperation class. It doesn't have to refer only to methods of a functional interface.

Which of the following interface is a functional interface?

Runnable, ActionListener, Comparable are some of the examples of functional interfaces.

What does “the target type of this expression must be functional interface” mean?

By now you may have guessed that resolving this error " the target type of this expression must be a functional interface " means functional interface should have only one abstract method. So we need to delete the second method.

What is the target type of this expression in Java 8?

Java 8 - The target type of this expression must be a functional interface. Java 8 Error-The target type of this expression must be a functional interface. If you are working in Java 8 and getting the following compilation error – to a reference which is not a Functional Interface. .

What is the difference between functional interface and lambda expression?

Thus the functional interface specifies its target type. As you can see Functional interface is an interface with only one abstract method and lambda expression provides implementation of the abstract method defined by the functional interface.

What is a functional interface in Java?

A functional interface is an interface with only one abstract method. A functional interface is also known as SAM type where SAM stands for (Single Abstract Method). Lambda expression is an instance of the functional interface and provides implementation of the abstract method defined by the functional interface.


1 Answers

Because that's a method reference, the usage is a bit different:

 Consumer<String> c = System.out::println; 

The argument that the consumer takes (s) will be still passed to the println method.

here is Oracle's tutorial on this.

like image 83
Eugene Avatar answered Sep 21 '22 09:09

Eugene