What does the double colon "::" mean in Java?

Today I would like to share with you a new feature in JDK8: the double colon: "::". The double colon "::" is the method reference in Java, is one of the ways to write Lambda expressions in Java 8, a method of executing methods. Simplifies the redundant code of our Java development to a certain extent.

There are generally 6 usage scenarios for double colon (::):

type Quote syntax Case
Reference static method Class name::static method name Integer::parseInt
Reference to a specific object instance method Object::static method name System.out::println
Instance methods that reference any object of a specific type Specific type::instance method name String::compareToIgnoreCase
Reference to an instance method of a superclass (parent class) supre::method name top::fun
Reference class constructor Class name::new ArrayList::new
Reference array constructor Array type[]::new String[]::new

The above content is too official. Let’s briefly look at two small examples:

1. Lambda表达式表达式:
person -> person.getName();
可以替换成:
Person::getName

2. Lambda表达式表达式:
() -> new HashMap<>();
可以替换成:
HashMap::new

In actual development, we may also encounter many operations that use double colons to call methods.

public class Test07 {
    
    
    public static void a(){
    
    
        System.out.printf("a1");
    }
    public static void a(String s){
    
    
        System.out.printf(s);
    }
    public static void main(String[] args) {
    
    
        Runnable runnable=Test07::a;
        runnable.run();
    }
}

At this time, can you see that a method a is being called? Let’s run it directly and see the results:
Insert image description here

ExplanationRunnable corresponds to method a without parameters. Why is this?

The reason lies in Runnable. It is not difficult to trace its original code and find that it is a functional interface.
Insert image description here

There is only one parameterless run method in , which corresponds toRunnable runnable=Test07::a, which is the parameterless method a< /span>
If we want to use method a with parameters, what should we use?

public class Test07 {
    
    
    public static void a() {
    
    
        System.out.printf("a1");
    }

    public static void a(String s) {
    
    
        System.out.printf(s);
    }

    public static void main(String[] args) {
    
    
        Consumer<String> consumer=Test07::a;
        consumer.accept("调用有参的方法a");
    }
}

Insert image description here

You can also find through its source code that Consumer is also a functional interface, which provides an accept method with parameters. This method corresponds toTest07::a is method a with parameters. After execution, you can see the results as follows:
Insert image description here

Seeing this, everyone should have some understanding of what a method reference is. Its essence is that Runnable or Consumer points to a specific method. As for which one to use, it depends on whether the pointed method has parameters.

Some people may have doubts, because our Consumer's accept can only receive one method, so the referenced method can only correspond to one parameter. What if there are multiple parameters, like the following method:

    public static void a(String s1,String s2) {
    
    
        System.out.printf(s1+s2);
    }

JDK has many built-in default functional interfaces, such asBiConsumer

    public static void main(String[] args) {
    
    
        BiConsumer<String,String> biConsumer=Test07::a;
        biConsumer.accept("测试","数据");
    }

Insert image description here

You can see that it can receive two parameters. If the parameters are greater than or equal to two, it is recommended to encapsulate the parameters into an object and pass the parameters through the object.

The above content has no return result, because neither the run method of Runnable nor the accept method of Consumer has a return value. If we need a return value, we can use it Callable
Insert image description here

It is not difficult to find through the source code that Callable is also a functional interface, which provides a call method without parameters and provides a return value.

public class Test07 {
    
    
    public static String b(){
    
    
        return "b";
    }
    public static void main(String[] args) throws Exception {
    
    
        Callable<String> runnable=Test07::b;
        String s=runnable.call();
        System.out.printf(s);
    }
}

Guess you like

Origin blog.csdn.net/zhiyikeji/article/details/133647992