In Java 8, a new special type of operator is added named as double (::) operator or method reference operator. Why we called it a method reference operator because this double (::) operator or method reference operator is used to call methods by the help of the class in which the method is declared.
As we know that lambda expression is one way to write more readable and concise code. Now with the help of method reference, you can write one step more readable and concise code. The main purpose of method reference is to write clean, concise, and more readable code. Method reference also supports the code reusability. Let's take an example and understand it.
Functional Interface: InfectedCountry.java
A concrete class contains some methods: Covid19Stats.java
A demo class for method reference: MethodReference.java
In the above example, for InfectedCountry.java functional interface we have written three lambda expressions. In the first lambda expression, we directly providing an implementation for the SAM(Single Abstract Method) interface and in the second one we are just calling another method inside the lambda expression and that looks more clean and concise from the first one.
Now, look at the third one in which we are using the method reference. The third one is one step more clear, concise, and readable. Also, it is reusing the already defined method for providing the implementation for the SAM(Single Abstract Method) interface. One thing you should always remember while writing lambda expression using method reference is that the SAM and the method you are going to use as an implementation for your SAM should have the same method signature.
Method references are of four types (i) reference to a static method (ii) reference to an instance method of a particular object (iii) reference to an instance method of an arbitrary object of a particular type (iv) reference to a constructor.
The above example explained the reference to a static method because we referencing a static method with the help of its class name. The syntax used for this is "ContainingClass :: staticMethodName". Now let's discuss the other method reference types left with help of mentioned below example.
Covid19CountryInfoDto.java
MethodReferenceDemo.java
In the above example, Covid19CountryInfoDto.java contains some methods. These methods are used as implementations for the special type of lambda expressions which are written in the MethodReferenceDemo.java class. If you noticed one thing in the above example for the method reference type (ii) reference to an instance method of a particular object, you needed an instance of the class "Covid19CountryInfoDto" to refer to instance methods of this class. The syntax for type (ii) is "containingObject :: instanceMethodName".
Now (iii) reference to an instance method of an arbitrary object of a particular type, is explained in the above example with the help of a String array. The objects present inside the String array are arbitrary objects of type String. So the type "String" is used to refer the methods present inside the class String. The syntax used for this "ContainingType :: methodName".
Now the last one (iv) reference to a constructor, is explained by creating a thread object with the help of constructor reference. In this example, a pre-defined functional interface called Function<T, R> is used where "T" is for the input type and "R" is for the return type. With the help of functional interface Function<T, R> a special type of lambda expression created using constructor reference which accepts a Runnable type of object and returns a Thread type of object. This special type of lambda expression is exactly the same as the Thread constructor present inside Thread class which accepts Runnable type object and returns Thread object. The syntax used for this is "ClassName :: new" The thread is created in the above example in both cases without using constructor reference and with constructor reference.
Now I am ending up this post and will update it if I will find something new about the method reference operator. Also, you can share your queries and feedback in the comment section below. Keep study guys and share with your friends and colleague also. Thank you so much.
Comments
Post a Comment