java 8 method reference

Keywords: Lambda

If the code block of Lambda expression has only one line of code, the program can omit the curly braces of the code block of expression. Moreover, if there is only one line of code, the reference of method and the reference of constructor can also be used

Here are four examples of references

public class function {

    interface Converter{
        /*The method of converting String to int*/
        int converter(String from);

    interface Mytest3{
        String mytest3(String a,int b,int c);

    interface Mytest4{
        JFrame win(String title);

    public static void main(String[] args) {
         * 1,Reference class method
        //Create an anonymous inner class implementation of Converter object
        Converter converter = new Converter(){
            public int converter(String from) {
                return Integer.parseInt(from);

        //Creating a Converter object Lambda implementation
        converter = from->Integer.valueOf(from);
        //Method reference instead of Lambda expression
        converter = Integer::valueOf;

        //Call converter implementation
        int number =converter.converter("5");

         * 2,References instances of specific objects
        //Creating a Converter object Lambda implementation
        converter = from->"HELLO WORLD".indexOf(from);
        //References instances of specific objects in place of Lambda expressions
        converter = "HELLO WORLD"::indexOf;

        number =converter.converter("WO");
         *3,An instance method that references a class of objects
        Mytest3 test = (a,b,c)-> a.substring(b, c);

        //References instances of specific objects in place of Lambda expressions
        /* When referencing an instance method of an object of a class, please note:
         * 1,The first parameter of the implemented method in the functional interface is the caller
         * 2,All the following parameters are passed to the modified parameter as the method
         * */
        test = String::substring;

        String subString=test.mytest3("HELLO WORLD", 2, 5);

        System.out.println("The intercepted string is="+subString);
         * 4,Reference constructor
        Mytest4 jfMytest4=title-> new JFrame(title);
        //Reference constructor instead of Lambda expression
        jfMytest4= JFrame::new;

        JFrame"My window");

The difference and connection between lambda and anonymous inner class
lambdab expression is a simplification of anonymous inner class, so it can replace some anonymous inner classes.
1. Can directly access local variables and member variables of external classes
2. The created anonymous object can be called directly, and the default method inherited from the interface can be used

1. Anonymous inner classes can create instances for any interface no matter how many methods there are in the interface, anonymous inner classes only need to be implemented, and lamdba expressions can only create instances for functional interfaces
2. In the abstract method implemented by anonymous inner class, it is allowed to call the default method of the interface. When the code block of lambda expression is not allowed to call the default method defined in the interface

Posted by bateman on Tue, 05 May 2020 09:46:35 -0700