Method References
Description
A lambda expression represents an anonymous function defined in a functional interface.
A method reference creates a lambda expression using an existing method.
The general syntax for a method reference is
Qualifier::MethodName
Two consecutive colons act as a separator.
The MethodName
is the name of the method.
Qualifier
tells where to find the method reference.
Example
For example we can use String::length
to reference the length method from String
class.
Here String
is the qualifier and length
is the method name.
We only need to specify the method name.
There is no need to specify the parameter types and return type for the method.
The target type of the method reference is a functional interface. It determines the method's signature and resolves the overloaded method if necessary.