Java – how to assign method reference values to runnable
•
Java
I discussed the problem of Java 8 runnable
public static void main(String[] args) {
Runnable r1 = Test::t1;
Runnable r2 = Test::t2;
Runnable r3 = Test::t3;
}
public static void t1() {
}
public static String t2() {
return "abc";
}
public static String t3(String t) {
return t;
}
As the code shows, I understand that R1 is correct and R3 is wrong, but I don't understand why R2 is also right Can someone help me understand
Solution
Since section 15.13 2 of the JLS, R2 is a fine, including:
Basically, writing T2 () is valid; And just ignore the return value, so it is valid to create a method reference that calls the method and ignores the return value
T3 is invalid because you have to provide a parameter and runnable has no parameters, so nothing can be "passed" to the method
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
二维码
