Capturing versus non-capturing Lambdas captured and non-captured lambda expressions

Source: Internet
Author: User

capturing versus non-capturing lambdas

Lambdas is said to being "capturing" if they access a non-static variable or object that is defined outside of the lambda b Ody. For example, this lambda captures the Variablex:

int x = 5; return y x + y;


In order for this lambda declaration to is valid, the variables it captures must be "effectively final". So, either they must is marked with thefinalmodifier, or they must not being modified after they ' re assigned.

Whether a lambda is capturing, or not have implications for performance. A non-capturing Lambda is generally going to being more efficient than a capturing one. Although this isn't defined in any specifications (as far as I know), and we shouldn ' t count on it for a program ' s Corre Ctness, a non-capturing lambda only needs to be evaluated once. From and on, it'll return an identical instance. Capturing lambdas need to being evaluated every time they ' re encountered, and currently that performs much like instantiating A new instance of an anonymous class.


captured and non-captured lambda expressions

When a lambda expression accesses a non-static variable or object defined outside the body of a lambda expression, the lambda expression is called "captured." For example, the following lambda expression captures the variable x:

int x = 5; return y x + y;
to ensure that the lambda expression declaration is correct, the variable it captures must be "valid final". So either they need to be marked with final modifier symbols, or they are guaranteed to not change after assignment.

Whether a lambda expression is captured and is quietly related to performance. A non-trapping lambda is usually more efficient than capturing, although there is no written specification (as far as I know), and there is no reason to expect it to do anything for the correctness of the program, and a non-capturing lambda needs to be computed only once. Then each time it is used, it returns a unique instance. The captured lambda expression needs to be recalculated each time it is used, and from the current implementation it is much like instantiating an instance of an anonymous inner class.

performance of Lambda

Documentation for Oracle's performance comparison: JDK 8:LAMBDA Performance Study, a detailed and comprehensive comparison of performance differences between LAMBDA expressions and anonymous functions.

The Lambda Development Group also has a PPT, which also describes the performance of lambda, including capture and non-capture scenarios. Lambda has the worst case performance internal class, and the good is certainly higher than the intrinsic class performance.

Java 8 Lambdas-they is fast, very fast.
Java 8 Lambda Performance Comparison


Https://github.com/LMAX-Exchange/disruptor/wiki/Getting-Started also has a note on this:

This would create a capturing lambda, meaning that it would need to instantiate an object to hold the ByteBuffer bb variable as it Passes the lambda through to the call publishEvent() . This would create additional (unnecessary) garbage, so the "call" passes the argument through to the lambda should is PR eferred If low GC pressure is a requirement.

Give that method references can be used instead of anonymous Lamdbas it's possible to rewrite the example in this fashion .



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Capturing versus non-capturing Lambdas captured and non-captured lambda expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.