How does method references work under hood when there are multiple arguments

Tushar Baviskar :

How does compiler ensure that equivalent lambda for below statement

BinaryOperator<String> concatOperator = String::concat; 

is

BinaryOperator<String> concatOperator = (resultString, inputString) -> resultString.concat(inputString);

and not

BinaryOperator<String> concatOperator = (resultString, inputString) -> inputString.concat(resultString);
Andrew Tobilko :

This behaviour is well-documented in the JLS

15.13.3. Run-Time Evaluation of Method References

If the compile-time declaration is an instance method, then the target reference is the first formal parameter of the invocation method. Otherwise, there is no target reference.

If the compile-time declaration is an instance method, then the arguments to the method invocation expression (if any) are the second and subsequent formal parameters of the invocation method. Otherwise, the arguments to the method invocation expression are the formal parameters of the invocation method.

and it seems reasonable and intuitive. If you take a method with arity n (n > 2), it becomes obvious that the target reference should be the first parameter, not the last, not the one in the middle.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=310064&siteId=1