-1

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

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Vipin CP
  • 3,642
  • 3
  • 33
  • 55
  • 2
    Why inventing new names? It’s basically equivalent to `f -> f` which is a short-hand for `(Employee f) -> { return f; }`. What’s your actual question? Where in this code is an “object calls itself” happening, as your question’s title suggests? – Holger Sep 02 '19 at 11:53
  • 3
    It seems you are confused by the `->` operator, which you are interpreting like a C++/C pointer dereference. – Raedwald Sep 02 '19 at 12:21
  • 1
    See also https://stackoverflow.com/questions/28032827/java-8-lambdas-function-identity-or-t-t – Raedwald Sep 02 '19 at 12:21

2 Answers2

3

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.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
1

In a context where a Function<Employee, Employee> is accepted, then yes, e -> e will behave the same as the implementation you provided. The lambda isn't executed until it is passed an argument, in which case it just returns that same argument.

claesv
  • 2,075
  • 13
  • 28