When lambdas become objects

Brian Goetz brian.goetz at oracle.com
Tue Nov 27 21:05:57 PST 2012


>> Implementations are permitted to do so, and ours (mostly) does.  But this is not guaranteed and you should not assume that it does.
>
> When you say I shouldn't assume that it does, you mean that I
> shouldn't rely on that behavior for my program's correctness, right?

Correct.

>>> Does every invocation of isFunActivity() return a separate Predicate
>>> instance?  Or, would a given FunDetector instance always return the same
>>> Predicate instance?
>>
>> Because isFun is an instance method, you have to capture the receiver variable 'this', so each capture is likely to result in a new object. (Note you could have written this both more compactly and more explicitly as "return this::isFun".)
>
> Are there plans to eventually do some sort of instance-level caching &
> reuse of these lambdas, when the lambdas only capture 'this', either
> later in the JDK8 development cycle or else in a future version of
> Java?

It's definitely out of scope for 8.  While its possible we might 
consider things like this in the future, realistically there are much 
higher-leverage optimizations where we'd likely put our efforts first. 
(Such as, reducing the cost of lambda capture in the first place.)



More information about the lambda-dev mailing list