Is lambda in fact an anonymous class?

mikeProgrammer :

I am reading Effective Java and wondering about differences between lambda and anonymous class. I know that lambda can only be used with interfaces with single method i.e. Functional Interfaces and in lambda you cannot obtain reference to itself so the keyword this relates to class in which lambda is specified when in anonymous class keyword this relates to body of an anonymous class. But I don't know what lambda really is. I guess that this is only an expression that creates an instance of anonymous class which implements Functional Interface so this is only syntactic sugar with some limitations when comparing to normal anonymous class but thanks to it we can get rid of boilerplate and make our code more readability. On the other hand here we can read about performance differences what could suggest that lambda in fact is not an instance of anonymous class which implements Function Interface.

Basil Bourque :

No, lambdas != anonymous inner classes

Lambdas in Java replace many of the common uses of anonymous inner classes. The result is much more compact, readable, and obvious code.

No, the implementation of lambdas is not based on anonymous inner classes.

For more discussion, see this Question on a sibling site of Stack Overflow.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126189&siteId=1