LearnFrenzy
Sign in Take a free test

Lambda Expressions · Find the Error · Question 3

Error: a lambda with too many parameters Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        Function<String, Integer> len = (a, b) -> a.length();
        System.out.println(len.apply("java"));
    }
}
  • A length is not a method
  • B Function.apply takes one argument, but the lambda declares two parameters; use a BiFunction or one parameter Correct
  • C Function needs three type arguments
  • D a must be declared String
Correct Answer: Option B

javac: "incompatible parameter types in lambda expression".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links