5

I am reading couple of blogs and answers as well on lambda expression provided in Java 8.

I am not able to figure out, is there any run-time benefit of unit lambda expression or not?

I have copied following text from different sources which are too much confusing to me.

One answer is saying -

"lambdas do NOT create a new scope, they share the same scope as the enclosing block/environment"

In the one blog -

"There are no runtime benefits of using lambda expressions, so I will use it cautiously because I don’t mind writing few extra lines of code."

From one other blog -

"one more benefit lambda expressions Sequential and Parallel Execution Support by passing behavior in methods"

So there is lots off confusion for me. Please help me to clear this so I can avoid keeping wrong approach in mind before digging more in to this stuff.

I have run following code and I can say the lambda expression in the following code is just replacement for the anonymous inner class and its running on main thread.

List<Integer> list = new ArrayList<>();
list.add(12);
list.forEach(V -> {
    System.out.println(V);
});

Are we reducing any time complexity or space complexity?

Amol Patil
  • 985
  • 2
  • 11
  • 43
  • 1
    this might make you some sense https://stackoverflow.com/questions/34585444/java-lambdas-20-times-slower-than-anonymous-classes – Shafin Mahmud Jan 22 '18 at 06:05
  • 1
    FYI, `V -> { System.out.println(V); }` can be simplified to `V -> System.out.println(V)`, which is basically equivalent to `System.out::println`. – shmosel Jan 22 '18 at 06:50
  • 1
    [Does a lambda expression create an object on the heap every time it's executed?](https://stackoverflow.com/q/27524445/2711488) may also help. – Holger Jan 22 '18 at 10:16
  • 1
    Also worth reading [Do lambda expressions have any use other than saving lines of code?](https://stackoverflow.com/q/47407180/2711488) – Holger Jan 22 '18 at 11:11
  • If you have a new question, [ask](https://stackoverflow.com/questions/ask) a new question, instead of redefining a more than a year old question. Time and space complexity have nothing to do with anything you wrote before your last sentence. – Holger May 29 '19 at 11:48
  • Hmm here is nothing unclear. And the answer is NO. See https://stackoverflow.com/a/48376002/1603952 -> this is your answer :) As stated in https://stackoverflow.com/a/48379603/1603952 as well ;) – Marvin Emil Brach May 29 '19 at 15:29

3 Answers3

6

lambdas do NOT create a new scope, they share the same scope as the enclosing block/ environment” is an almost correct statement (they do create a new scope, but not the way inner classes do), but doesn’t have anything to do with runtime performance. This has to do with correctness of the code.

Within an anonymous inner class, identifiers may get resolved through the lexical scope, finding a match in the surrounding scope, or by inheritance, finding a match in the class hierarchy of the anonymous inner class. The rules for resolving identifiers in this scenario are complex and easy to confuse.

Further, the body of an anonymous class creates a new scope that allows to create variables having the same name as local variables of the surrounding context, shadowing these variables.

In contrast, a lambda expression works like other expressions within the context they are written in. They do not inherit any members from the functional interface they will get converted to, they can not create new variables shadowing existing local variables and even this and super have the same meaning as within the surrounding context:

JLS§15.27.2. Lambda Body

Unlike code appearing in anonymous class declarations, the meaning of names and the this and super keywords appearing in a lambda body, along with the accessibility of referenced declarations, are the same as in the surrounding context (except that lambda parameters introduce new names).

So when you have the expressions x.foo(y) and () -> x.foo(y) within the same block, it will be obvious whether x and y will be the same x and y for both expressions and hence, it will be the same foo method in each case, which you can not say that simple for anonymous inner classes, as you have to analyze the entire inner class and its type hierarchy first.

This makes lambda expressions ideal for scenarios where you want to define a local function and, e.g. pass it to a method as a parameter, without even thinking about the actual interface being used. The interface itself does not influence the lambda expression beyond defining the functional signature.

But this also implies that there might be use cases of anonymous classes that can’t be covered by lambda expressions. But the purpose of lambda expressions isn’t to be a general replacement for anonymous inner classes.


When it comes to performance or easy of parallel processing, shmosel’s answer says it already. We can’t make such general statements without knowing which operation/problem we are looking at and which solutions we are actually comparing.

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765
5

lambdas do NOT create a new scope, they share the same scope as the enclosing block/environment

I'm not sure what your point is with this quote.

There are no runtime benefits of using lambda expressions

Relative to what? Lambdas may perform better than anonymous classes, but neither will outperform simple control flow statements.

one more benefit lambda expressions Sequential and Parallel Execution Support by passing behavior in methods

Streams and lambdas enable declarative programming, which makes parallelization more accessible. So using lambdas may indirectly improve performance.

shmosel
  • 49,289
  • 6
  • 73
  • 138
-2

anonymous inner class is compiled into extra .class file, but lambda is translated dynamically. Here is a detailed post: How will Java lambda functions be compiled?

Alex M981
  • 2,264
  • 14
  • 24
  • will extra .class file will create any impact on runtime? – Amol Patil Jan 22 '18 at 06:12
  • @AmolPatil IMO, both ways have their pros and cons. Using anonymous class creates the extra file, so JVM has to go out and look for it (then loads the class). On the other hand, because lambda is interpretted at runtime, the interpretation will definitely cost a bit as well (I'm not an expert but this is what I understood). Therefore, these will only be obvious if you benchmark on your target system. – Jai Jan 22 '18 at 06:19
  • thanks @Jai , but I thought class get load at the time jvm get lunched so I think extra .class file will not create impact on run-time. Please clear me if i am wrong. About lambdas, I am unaware that lambdas are interpreted at runtime – Amol Patil Jan 22 '18 at 06:28
  • 1
    @AmolPatil well, `.class` files get loaded at startup time or at their first use, which is already an operation with an impact, even if it is a one-time thing only. Sometime, the initialization time is relevant, but of course, for a server running for weeks, that rarely matters. – Holger Jan 22 '18 at 10:13