A very good article i found here:
References to bound non-static methods
A bound non-static method reference refers to a non-static method that's bound to a receiver object. Its syntax is objectName::instanceMethodName, where objectName identifies the receiver and instanceMethodName identifies the instance method. An example is s::trim. Listing 2 demonstrates a bound non-static method reference.
Listing 2. MRDemo version 2: A bound non-static method reference
import java.util.function.Supplier;
public class MRDemo
{
public static void main(String[] args)
{
String s = "The quick brown fox jumped over the lazy dog";
print(s::length);
print(() -> s.length());
print(new Supplier<Integer>()
{
@Override
public Integer get()
{
return s.length(); // closes over s
}
});
}
public static void print(Supplier<Integer> supplier)
{
System.out.println(supplier.get());
}
}
Listing 2's main() method assigns a string to String variable s and then invokes the print() class method with functionality to obtain the string's length as this method's argument. print() is invoked in method reference (s::length -- length() is bound to s), equivalent lambda, and equivalent anonymous class contexts.
I've defined print() to use the java.util.function.Supplier predefined functional interface, whose get() method returns a supplier of results. In this case, the Supplier instance passed to print() implements its get() method to return s.length(); print() outputs this length.
s::length introduces a closure that closes over s. You can see this more clearly in the lambda example. Because the lambda has no arguments, the value of s is only available from the enclosing scope. Therefore, the lambda body is a closure that closes over s. The anonymous class example makes this even clearer.
Compile Listing 2 and run the application. You'll observe the following output:
44
44
44
References to unbound non-static methods
An unbound non-static method reference refers to a non-static method that's not bound to a receiver object. Its syntax is className::instanceMethodName, where className identifies the class that declares the instance method and instanceMethodName identifies the instance method. An example is String::toLowerCase.
String::toLowerCase is an unbound non-static method reference that identifies the non-static String toLowerCase() method of the String class. However, because a non-static method still requires a receiver object (in this example a String object, which is used to invoke toLowerCase() via the method reference), the receiver object is created by the virtual machine. toLowerCase() will be invoked on this object. String::toLowerCase specifies a method that takes a single String argument, which is the receiver object, and returns a String result. String::toLowerCase() is equivalent to lambda (String s) -> { return s.toLowerCase(); }.
Listing 3 demonstrates this unbound non-static method reference.
Listing 3. MRDemo version 3: Unbound non-static method reference
import java.util.function.Function;
public class MRDemo
{
public static void main(String[] args)
{
print(String::toLowerCase, "STRING TO LOWERCASE");
print(s -> s.toLowerCase(), "STRING TO LOWERCASE");
print(new Function<String, String>()
{
@Override
public String apply(String s) // receives argument in parameter s;
{ // doesn't need to close over s
return s.toLowerCase();
}
}, "STRING TO LOWERCASE");
}
public static void print(Function<String, String> function, String
s)
{
System.out.println(function.apply(s));
}
}
Listing 3's main() method invokes the print() class method with functionality to convert a string to lowercase along with the string to be converted as the method's arguments. print() is invoked in method reference (String::toLowerCase -- toLowerCase() isn't bound to a user-specified object), equivalent lambda, and equivalent anonymous class contexts.
I've defined print() to use the java.util.function.Function predefined functional interface, which represents a function that accepts one argument and produces a result. In this case, the Function instance passed to print() implements its R apply(T t) method to return s.toLowerCase(); print() outputs this string.
Although the String part of String::toLowerCase makes it look like a class is being referenced, only an instance of this class is referenced. The anonymous class example makes this fact more obvious. Note that the anonymous class example shows that the lambda receives an argument; it doesn't close over parameter s and is therefore not a closure.
Compile Listing 3 and run the application. You'll observe the following output:
string to lowercase
string to lowercase
string to lowercase