Home java What does the :: operator mean?

What does the :: operator mean?

Author

Date

Category

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.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions