I am learning JavaFX. Bumped into operator ::. Please explain what it means.
Example:
public static LocalDate parse (String dateString) {
try {
return DATE_FORMATTER.parse (dateString, LocalDate :: from);
} catch (DateTimeParseException e) {
return null;
}
}
Answer 1, authority 100%
This is a method reference , a new Java 8 language construct
For example:
public static void main (String [] args) {
List & lt; String & gt; list = Arrays.asList ("str1", "str2", "str3");
list.forEach (System.out :: print);
}
The following will happen: the List interface inherits from Iterable, which has a method forEach (Consumer & lt ;? super T & gt; action)
.
Consumer – functional interface, with one method accept (T t)
, which takes one parameter. In this case, consumer is the print
method of the System.out
class that accepts a string.
Functional interface – an interface that has only 1 abstract method.