Search code examples
javalambdajava-8anonymous-class

object calls itself using lambda, confused


Confused about java 8 lambda expression.

below is an old java way of implementation of functional interface method;

 Function<Employee, Employee> employeeFunc=new Function<Employee,Employee>(){

            public Employee apply(Employee f) {
                return f;
            }
        };

Is this same as e->e ? Say e is an Employee.

what is happening when object->object executes? please explain


Solution

  • Roughly speaking, it's very similar to e -> e, or Function.identity().

    As the JLS put it,

    15.27.4. Run-Time Evaluation of Lambda Expressions

    At run time, evaluation of a lambda expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object. Evaluation of a lambda expression is distinct from execution of the lambda body.

    It doesn't "call itself", it's a lambda that takes an object and returns that object without any intermediate action.