Why do equivalent lambda expression and method reference behave differently when capturing static field value?

前端 未结 1 1604
忘掉有多难
忘掉有多难 2021-01-05 05:23

I\'m a little bit confused about Java lambdas and method references behaviour. For ex., we have this code:

import java.util.function.Consumer;

public class          


        
1条回答
  •  不思量自难忘°
    2021-01-05 05:47

    In the lambda expression, the sBuilder field is captured, but not evaluated. It will only be evaluated when the corresponding function interface method is invoked. At that point, the sBuilder references the new instance created and assigned to the field with

    sBuilder = new StringBuilder("2");
    

    In the method reference, the sBuilder field is evaluated immediately to produce a Consumer instance. That value references the instance created in the static initializer

    private static StringBuilder sBuilder = new StringBuilder("1");
    

    and the Consumer will operate on that one. You print the new one.


    From the Java Language Specification, concerning the Run-Time Evaluation of Method References

    The body of an invocation method depends on the form of the method reference expression, as follows:

    If the form is ExpressionName :: [TypeArguments] Identifier or Primary :: [TypeArguments] Identifier, then the body of the invocation method has the effect of a method invocation expression for a compile-time declaration which is the compile-time declaration of the method reference expression. Run-time evaluation of the method invocation expression is as specified in §15.12.4.3, §15.12.4.4, and §15.12.4.5, where:

    • The invocation mode is derived from the compile-time declaration as specified in §15.12.3.

    • The target reference is the value of ExpressionName or Primary, as determined when the method reference expression was evaluated.

    • The arguments to the method invocation expression are the formal parameters of the invocation method.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题