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,

System.out::println

In order to understand better, you must know about functional interface. Please check blog Java lambda and functional programming. Java 8  method (functional interface) are just like objects, They can be passed as parameters and can be returned from another method. So why not assign Java methods to a new another variable.  Something like javascript.

var squareFunc1 = function(var x){
return x*x;
}
var squareFunc2 = squareFunc1 ;

Here is how we can do the same in Java.

interface Utility{
 int biggerNumber(int x,int y);
}
Utility utility = Math::max; //Assign method definition to another signature matching method.

utility.biggerNumber(3,8) // returns 8

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

public interface StringSearch{
    public int search(String s1, String s2);
}
public class MyClass{
    public static int find(String s1, String s2){
        return s1.lastIndexOf(s2);
    }
}
StringSearch search = MyClass::find; // Line 9.

Parameter method reference

Here functional interface with two parameters gets the definition from a method having only one parameter. Stange !!.

public interface StringSearch{
    public int search(String s1, String s2);
}

str1.indexOf(String str2) // line 4

StringSearch search= String::indexOf;

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.

public interface StringSearch{
    public int search(String s1, String s2);
}
public class MyClass{
    public int find(String s1, String s2){
        return s1.lastIndexOf(s2);
    }
}
MyClass myClass = new MyClass();
StringSearch search = myClass::find; // Line 9.

Constructor reference.

This is the one, It can be very handy in factory patterns.

public interface StringFactory {
    public String create(char[] val);
}
StringFactory factory = String::new;

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.

Utility utility = Math::max
Or 
Utility utility = (x,y) -> return x > y ? x: y;