Lambda for Method reference Or Java Double colon operator
What is the Java 8 double colon operator ? You must have seen some short code like,
Here is how we can do the same in Java.
So above you can see, We have created an interface with only one abstract method having signature same as the signature of Math.max method. Later we have assigned the definition of Math.max to Utility. Which mean the definition is assigned is to Utility .biggerNumber. Now we can call the method as shown in the last line. There are different ways to where we can use double operator lambda operator
Static method reference
Assign static method to a matching functional interface. as shown in line 9
Parameter method reference
Here functional interface with two parameters gets the definition from a method having only one parameter. Stange !!.
Not so strange, The object type on which method is called and method parameters are matched to the functional interface. In this StringSearch.search take two param and params are matched to str1 and str2 from line 4.
Instance method reference
This one is the same as the static method reference.
This is the one, It can be very handy in factory patterns.
These were the few examples of how we can use Java lambda to reduce code length and make it more readable. Since these expressions represent function declaration hence these Java 8 double colon operator can be used in place of Java 8 arrow function. For example.