Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do "References to an Instance Method of an Arbitrary Object of a Particular Type" work with >2 args?

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::compareToIgnoreCase would 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 method a.compareToIgnoreCase(b).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Similarly, the method reference String::concat would invoke the method a.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

Oxford Learners Dictionaries

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading