lambda expressions and a method reference do not have a predefined type, these are poly expressions, as shown here . This means that their type is derived from the context in which they are used.
In your example, both of them will be legal, for example:
BiFunction<Person, Person, Integer> biFun = myComparisonProvider::compareByName;
Comparator<Person> comp = myComparisonProvider::compareByName;
But at the same time you cannot:
Arrays.sort(pers, biFun);
When you are actually trying to sort an array as follows:
Arrays.sort(pers, myComparisonProvider::compareByName);
At the bytecode level, which is Comparator:
// InvokeDynamic
Also note that this will be true:
Comparator<Person> comp = myComparisonProvider::compareByName;
System.out.println(comp instanceof Comparator);
You can enable the flag: -Djdk.internal.lambda.dumpProxyClasses=/Your/Path/Here
and see what this method reference is converted to:
final class Test$$Lambda$1 implements java.util.Comparator
compare ( , ):
public int compare(java.lang.Object, java.lang.Object);
Code:
4: aload_1
5: checkcast // class Test3$Person
8: aload_2
9: checkcast // class Test$Person
12: invokevirtual Test$ComparisonProvider.compareByName:(Test$Person;Test$Person;)I