Wednesday, February 6, 2019

Expressions

Q What is a Lambda Expression?

Java lambda expressions are new in Java 8. Java lambda expressions are Java's first step into functional programming. A Java lambda expression is thus a function which can be created without belonging to any class. A Java lambda expression can be passed around as if it was an object and executed on demand.

Java lambda expressions are commonly used to implement simple event listeners / callbacks, or in functional programming with the Java Streams API.

Q Why Comparator Interface despite having 2 abstract methods(compare and equals) is also a Functional Interface?

As per the Java Document

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.
Since the equals method in Comparator interface is inherited from the Object Class in Java so it does not count towards the interface's abstract methods.Hence this interface only has 1 abstract method which is compare we can use it as a Functional Interface.

No comments:

Post a Comment