LearnFrenzy
Sign in Take a free test

Lambda Expressions · Find the Error · Question 2

Error: assigning a lambda to Object Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

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

Main.java
public class Main {
    public static void main(String[] args) {
        Object task = () -> System.out.println("hi");
        System.out.println(task);
    }
}
  • A println cannot print lambdas
  • B A lambda needs a functional-interface target type; Object is not one – use Runnable task = … Correct
  • C Lambdas cannot print
  • D task must be final
Correct Answer: Option B

javac: "incompatible types: Object is not a functional interface". (Object o = (Runnable) () -> … would compile.)

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