I refer to the same method twice, but the links are different. See this example:
import java.util.function.Consumer; public class MethodRefTest { public static void main(String[] args) { new MethodRefTest(); } public MethodRefTest() { Consumer<Integer> a = this::method; System.out.println(a); Consumer<Integer> b = this::method; System.out.println(b); } public void method(Integer value) { } }
Conclusion:
MethodRefTest$$Lambda$1/250421012@4c873330 MethodRefTest$$Lambda$2/295530567@776ec8df
Is a method a reference nothing more than syntactic sugar for anonymous classes? If not, what should I do to always get the same method reference? (Besides storing the link once in a field to work with.)
(Appendix: I thought of method references as a prettier way to implement an observer, but with different references every time it is impossible to remove an observer from an observable after adding it.)
Is a method a reference nothing more than syntactic sugar for anonymous classes?
. , , .
, , ? ( .)
. . ( .)
,
?
JLS ,
(JLS 8, 15.13)
, , . , .
, , , , . , , JLS, , , .
, , . , @JohnKugelman, , , , .
() .
, toString() , , , - . , -, . - a==b.
toString()
a==b
, , . - , ?, JVM , HotSpot/OpenJDK , this::method - this. , , this::method .
this::method
this
Java 8?, , . , , - . , , , , - .
Source: https://habr.com/ru/post/1687049/More articles:creating a dictionary from a list with each element as a key: pairs of values โโin python - jsonC ++ External Functional Names Overlapping - c ++Does Hazelcast support SQL queries such as Apache Ignite? - sqlCounting the number of "NaN" (not zeros or spaces) in csv - pythonRegex Skipping Similar Results - regexSlack bot, register click message button - pythonparTraversable without creating sparks - parallel-processingFind the longest unique substring in a python string - pythonAngular 4.3 HttpClient with MockBackend - angularะกะฒะพะนััะฒะพ 'toPromise' ะฝะต ัััะตััะฒัะตั ะฒ ัะธะฟะต 'Observable - angularAll Articles