0 votes
in JAVA by
What is a method reference?

1 Answer

0 votes
by

Method reference is used to refer method of the functional interface. It is a compact and easy form of a lambda expression. Each time when you are using a lambda expression to just referring a method, you can replace your lambda expression with a method reference.

Below are a few examples of method reference:

(o) -> o.toString();

can become:

Object::toString();

A method reference can be identified by a double colon separating a class or object name and the name of the method. It has different variations such as constructor reference:

String::new;

Static method reference:

String::valueOf;

Bound instance method reference:

str::toString;

Unbound instance method reference:

String::toString;

...