0
votes

I have read quite a considerable stuff about lambda. But their are subtle doubts:

Q1. Definition of lambda expressions.

  1. I didnt find clear definition on oracle page.
  2. This link says "A Java lambda expression is thus a function which can be created without belonging to any class."
  3. This link says "Lambda Expression is a concise representation of an anonymous function that can be passed around."
  4. This link says "Lambda expressions basically express instances of functional interfaces "

2nd and 3rd link does no say anything about functional interfaces. Fourth link sounds somewhat good, but misses to state "anonymous" aspect of lambda expressions. Can we define them more perfectly like this:

Lambda expressions are "syntactically concise expressions to define anonymous implementations of functional interfaces".

I believe specifying word "functional interface" in definition, should be enough to state that we implement its only method and it behaves like function, instead of class, even though are anonymous classes.

Q2. Do we differentiate between lambda and lambda expressions?

I feel there is a difference, because lambda expressions are can be used to define both lambda which are stateless and closures which are stateful (i.e. access variables in outer lexical scope). Seems that, thats why oracle link does not use word "lambda" without "expression". However that is not the case with other links.

Q3. This link says, with lambda expressions, "We don’t need to write a lot of boilerplate like we do for anonymous classes." Now this sounds like "Lambda expressions are like anonymous classes, its just that", "we don’t need to write a lot of boilerplate like we do for anonymous classes." I feel we cannot straight compare lambda expressions with anonymous classes, for major difference is concept anonymous class has no connection with functional interfaces, while lambda expressions are strictly dealing with functional interfaces. So anonymous classes can have any number of methods, but lambda expressions can only define single method. Am right with this?

1
If you have three questions, ask three focused questions. As in, make three posts. Do not lump everything in together, just because they're on the same topic.Michael
Q1) docs.oracle.com/javase/tutorial/java/javaOO/… Q2) No Q3) Not a question. They are very comparable, since they both fulfill the same role of passing functions as arguments. Almost every single-method anonymous class should be converted to a lambda.Michael
Q1) Definition of Lambda Expressions Q2) Other links are not official documents. Q3) You can write every lambda expression as anonymous inner class. And that has much more boilerplate. And if we CAN use lambda expressions, it saves a lot of boilerplate.Johannes Kuhn

1 Answers

0
votes

For question 1, yes you can define it like that for everyday purposes. But under the hood, lambda expressions are syntactic sugar for good old methods. See this link. The body of the lambda is converted to a static method, and there is an invokedynamic instruction.

For question 2, "lambda" in this context is just short for "lambda expression", as far as I'm concerned. You wouldn't want such a long word in conversational speech, right? In other contexts, well, "lambda" can refer to the Greek letter λ.

For question 3, I would say that lambda expressions are alternatives to writing anonymous classes when you need a functional interface. On a high level of abstraction, they both can represent instances of functional interfaces. But if you need an instance of an abstract class or a non-functional interface, then lambdas can't be used.