How do so-called "References to an Instance Method of an Arbitrary Object of a Particular Type" work when there are more than two args? I found a relevant tutorial page, but it’s not explicit on that
The following is an example of a reference to an instance method of an arbitrary object of a particular type:
String[] stringArray = { "Barbara", "James", "Mary", "John", "Patricia", "Robert", "Michael", "Linda" }; Arrays.sort(stringArray, String::compareToIgnoreCase);The equivalent lambda expression for the method reference
String::compareToIgnoreCasewould have the formal parameter list (String a, String b), where a and b are arbitrary names used to better describe this example. The method reference would invoke the methoda.compareToIgnoreCase(b).Similarly, the method reference
String::concatwould invoke the methoda.concat(b).
I made a simple experiment, and it seems it work as follows: take the first arg and invoke the method on the rest of the args
// these are equivalent
TernaryOperator<String> ternaryOperator = (el1, el2, el3) -> el1.replaceAll(el2, el3);
TernaryOperator<String> ternaryOperator = String::replaceAll;
interface TernaryOperator<T> {
T apply(T el1, T el2, T el3);
}
However, I’m confused how it squares with the word "arbitrary". If it’s always the first arg, it’s not that arbitrary after all
(of an action, a decision, a rule, etc.) not seeming to be based on a reason, system [if it’s always the first arg, it seems like a system to me] or plan and sometimes seeming unfair
If it’s not documented, then it may technically be unsafe to rely on behavior of "References to an Instance Method of an Arbitrary Object of a Particular Type" in production projects
So what is it?
>Solution :
You misunderstood the English. “Arbitrary” does not mean an "arbitrary parameter" will be the invocation target. Read it again – it says "arbitrary object" – the object that is the invocation target is arbitrary. Any object of that type can be passed to the first parameter, so it’s "arbitrary".
See the Java Language Specification for more details. The most relevant part is probably this part (emphasis mine)
If the form is
ReferenceType :: [TypeArguments] Identifier, the body of the invocation method similarly 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.
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.