:: (double colon) operator in Java 8

前端 未结 17 3212
旧时难觅i
旧时难觅i 2020-11-21 11:10

I was exploring the Java 8 source and found this particular part of code very surprising:

//defined in IntPipeline.java
@Override
public fin         


        
17条回答
  •  孤城傲影
    2020-11-21 12:06

    :: is a new operator included in Java 8 that is used to refer a method of an existing class. You can refer static methods and non-static methods of a class.

    For referring static methods, the syntax is:

    ClassName :: methodName 
    

    For referring non-static methods, the syntax is

    objRef :: methodName
    

    And

    ClassName :: methodName
    

    The only prerequisite for referring a method is that method exists in a functional interface, which must be compatible with the method reference.

    Method references, when evaluated, create an instance of the functional interface.

    Found on: http://www.speakingcs.com/2014/08/method-references-in-java-8.html

提交回复
热议问题