Method Reference is a wonderful feature introduced in Java 8. Apart from taking advantage of functional programming, one of the biggest advantages in using a Lambda expression is to minimize the lines of code. Similarly, Method Reference(::) Java 8 also minimizes lines of code even more than Lambda. However, we use both of them in the presence of functional interfaces only. Moreover, It is also a concise and simpler form of lambda expression. In this article, we will also learn how to change a Lambda expression into a method reference. Let’s start discussing about ‘Method Reference(::) Java 8’ and it’s related concepts.
What is Method Reference(::)?
As we have seen in Lambda expression topic that we use lambda expressions to implement Functional interfaces with minimum lines of code and even to get better code readability. Similarly, we can use Method Reference(::) Java 8 to implement Functional interfaces with even lesser code again than lambda expressions and this time we get the benefit of code re-usability as well, because we don’t provide an implementation for functional interface. Instead, we provide reference to already existing method (with similar argument types) to simplify the implementation of the functional interface using a double colon (::) operator. This process of providing reference to pre-existing method is called Method reference.
From the above definition it is clear that while applying Method References our focus is always on the pre-existing methods which are well suited to the implementation of Functional Interface. In fact, pre-existing methods should have same arguments in number & type and there are no restrictions on their return type as such. Of course, here one thing is equally important to keep in mind that Method references & Lambda expressions both can only be used in context of Functional Interfaces.
In the above code snippet, Interface A, is a functional interface. The Single abstract method(SAM), getName(String name) of interface A has the same argument in number & type as the pre-existing method getInfo(String info) of class Test. Therefore, we can refer getInfo(String info) as an implementation of Functional interface A without any problem. Hence, the syntax for Method reference will be ClassName::methodName if it is static method.
Syntax to write Method References
Before knowing how to write them, let’s first categorize the Method Reference(::) Java 8. Here, syntax to write Method Reference(::) Java 8 is different in different cases. Typically, we can provide Method references in three ways, sometimes called the types of it.
- Static Method References
- Instance method/non-static Method References
- Constructor References
Now, let’s try to observe them one by one from below table.
Note : In addition, we have two more besides above three:
1) Method References of an arbitrary object of a given type
2) Method References from a super class method.
We will cover them separately in the subsequent sections.
Examples of each Type of Method Reference(::) Java 8
Furthermore, in this section we will see the example of each type one by one.
Method reference to a static method of a class :
In the above example, we have functional Interface A. We have implemented the single abstract method checkSingleDigit(int x) using Lambda expression in highlighted line. But while using Method reference, we have just referred the similar already existing static method isSingleDigit(int x) of class Digit.
Method reference to an Instance method of a class :
In the above example, we have functional Interface B. We have implemented the single abstract method add(int x, int y) using Lambda expression in highlighted line. While using Method reference, we have just referred the similar already existing method sum(int a, int b) of class Addition but after creating an object of the class this time.
When single abstract method’s return type is any Object, we will go with the constructor reference.
In the above code snippet, we have two Functional interfaces C & D. Interface C has abstract method with no arguments whereas D has with arguments. We will have same constructor reference code in both cases as shown in the highlighted lines of code. Also, if you run the code in your development environment, you will get the same output in each case.
Example of Reference to an Instance Method of an Arbitrary Object of a Particular Type
This type of Instance methods refers to a non-static method that are not bound to a receiver object. In this case we don’t need to create an object of a particular type.
In the above code snippet we have shown the three ways to implement a Functional Interface (Comparator) simultaneously. Of course, All of them outputs the same result.
Difference between reference to an Instance method of a particular object & an arbitrary Object of a given type
You might have some doubt on difference between both references, so now it’s right time to talk about them. To illustrate, let’s observe the examples given in the table below. I am sure you will get the clear idea on differences. There is no need to explain more about this.
Reference to super class & child class method using super & this keyword
Now we will see the use of super & this keyword in writing Method references accordingly.
As in the above code snippet, we have a functional interface ‘A‘ with Single Abstract method sayHello(). We have two classes SuperClass & ChildClass. Existing methods superHello() & childHello() are referred to write method references using super & this keyword respectively.
Furthermore, If you want to learn more about Method Reference(::) Java 8, kindly visit official Oracle Documentation.
What is the method reference in Java 8?
Method Reference is a new feature of Java 8. Using Method Reference, we provide a reference to already existing method (with similar argument types) to facilitate the implementation of a functional interface using a double colon (::) operator. This process of providing reference to pre-existing method is called Method reference. Moreover, Method Reference helps us to make shorter code as compared to a lambda expression.
What is the benefit of using method reference in Java 8?
What is difference between lambda expression and method reference?
Using Lambda Expression, we provide the implementation of a functional interface. In Method Reference, instead of providing an implementation of a functional interface’s method, we provide a reference to the pre-existing method using the double colon (::) operator.
What are three ways of implementing method reference?
Below are the three ways for method reference:
- Reference to a static method of a class
- Reference to an Instance method of a class
- Constructor Reference
Why do we need method references in Java?
As aforementioned, method references make the code, simpler, shorter, and more readable by just referencing the pre-existing methods rather than providing a custom implementation.
When should we use a method reference instead of a lambda expression?
We should use a method reference when we are calling an existing method without any additional logic or calculation in the lambda expression. If we need to perform some extra logic, a lambda expression may be more suitable.
Are method references faster?
Method Reference & Lambda has approximately similar performance. When we use lambda with Java 8 Streams, it provides better performance than the method reference.