Implicit type conversion of Java – lambda expressions
•
Java
Consider the following courses:
public void method() { test(() -> { }); } void test(Runnable a) { System.out.println("Test 1"); } void test(A a) { System.out.println("Test 2"); } interface A extends Runnable { }
Calling method () will result in test 2 output This means that the lambda expression () – > {} is implicitly converted to A. why?
Solution
This is the same standard rule for all overloads Java will choose the most specific applicable method.
Both methods accept parameters of a function interface type lambda expressions
() -> { }
Can be converted to these two types A is a subclass of runnable, so it is more specific Therefore, select the method with parameter type A
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码