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.
Asked
Active
Viewed 1,495 times
5

mikeProgrammer
- 427
- 4
- 10
-
5Check this link. https://softwareengineering.stackexchange.com/questions/195081/is-a-lambda-expression-something-more-than-an-anonymous-inner-class-with-a-singl – Sambit Jul 14 '19 at 16:53
-
7As your link has said, lambdas are indeed different from anonymous classes at the byte code level. Saying that "lambdas are just like anonymous classes" is just an analogy to get people who have no idea what lambdas are conformable with using lambdas. – Sweeper Jul 14 '19 at 17:02
-
Good question, too bad it didn't receive a good full-blown answer it deserves – Sergey Zolotarev Jun 30 '23 at 22:53
1 Answers
7
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.

Basil Bourque
- 303,325
- 100
- 852
- 1,154
-
1Thanks a lot, this link is exactly what I am looking for. In addition to > Lambdas in Java replace most (all?) uses of anonymous inner classes. the book stands that: >(...) there are a few things you can do with classes that you can’t do with lambdas. Lambdas are limited to functional interfaces. If you want to create an instance of an abstract class, you can do it with an anonymous class, but not a lambda. Similarly, you can use anonymous classes to create instances of interfaces with multiple abstract methods. Finally, a lambda cannot obtain a reference to itself. – mikeProgrammer Jul 14 '19 at 21:15
-
3@mikeProgrammer I think, it is more constructive not to see lambda expressions as a replacement for (some of the) anonymous classes, but as a means to define functions, which now allows to replace anonymous classes where they were used as a (we-don’t-have-better) means to define functions in the past. See also [this answer](https://stackoverflow.com/a/47408390/2711488). For developers who grew up with lambdas, there shouldn’t be any relationship between anonymous classes and lambda expressions. – Holger Jul 15 '19 at 12:29