Skip to main content

Double (::) Operator or Method Reference Operator

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

Popular posts from this blog

How to add an existing project to a GitHub Repository

  Add Existing project to a GitHub Repository Step 1: Login into your GitHub account and create a new repository. Step 2: Go to your project folder on your machine and open git bash. After that runs the command “ git init ”. This command initialized the empty git repository on your local machine. Step 3: Create a file “. gitignore ” using the command “ echo > .gitignore ”. Add the file extension or file name inside this file, those you don’t want to commit to your repository. Git will ignore all those files that will be present inside this file. Step 4: Add all the files to the staging area using the command “git add .” This command adds all the files to the staging area and all these files are now ready for the commit. And with the help of the command “git status” , you can check that all the files are inside the staging area. Also, you can see the file extension or the file name mentioned inside the “.gitignore”  are ignored by git. Step 5: Commit the files that are pre...

Lambda Expression

What is Lambda Expression? In java 8, a powerful concept is added named Lambda Expression.  Lambda expression is an anonymous function (a nameless function that does not have a modifier, return type, and even the type of its arguments).  Lambda expression is that you can pass to a method whose argument type is functional interface i.e. an interface that is either annotated by @FunctionalInterface  annotation e.g. interfaces from java.util.function package, or an interface with a single abstract method e.g. Comparable, Comparator, Runnable, Callable, ActionListener, etc.  Lambda expression allows passing customized code to the java method that was not possible before java 8. Although before Java 8 the only option was to wrap your code as an object and pass it using anonymous inner class to the method. Lambda expression defines parameters and code that executed using those parameters. Let's take the simplest example of the lambda expression (String input) -> ...

How to install IntelliJ IDEA on Windows?

System Requirements and steps to install IntelliJ IDEA on Windows System Requirements:  Step 1: Download IntelliJ IDEA. Click here. Step 2: Run the downloaded exe file and click on next. Step 3: Check the checkboxes mentioned in the below image and click on next and then hit the install button. Step 4: Final step is to reboot your system before start using IntelliJ IDEA. Thank you, guys. Please do comment below if there is any query or feedback.