Let's assume that we have the following Melon class and List of Melon:
public class Melon {
private final String type;
private int weight;
public static int growing100g(Melon melon) {
melon.setWeight(melon.getWeight() + 100);
return melon.getWeight();
}
// constructors, getters, setters, equals(),
// hashCode(), toString() omitted for brevity
}
List<Melon> melons = Arrays.asList(
new Melon("Crenshaw", 1200), new Melon("Gac", 3000),
new Melon("Hemi", 2600), new Melon("Hemi", 1600));
In a nutshell, method references are shortcuts for lambda expressions.
Mainly, method reference is a technique that's used to call a method by name rather than by a description of how to call it. The main benefit is readability.
A method reference is written by placing the target reference before the delimiter, ::, and the name of the method is provided after it.
We'll take a look at all four kinds of method references in the upcoming sections.