Home  >  Q&A  >  body text

In java8, what does the logical and & symbol mean when used on interface classes?

Recently I was reading the java source code related to lambda expressions, and found a lot of similar writing methods in the static methods of Comparator

java.util.Comparator
...
 public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) {
        Objects.requireNonNull(keyExtractor);
        return (Comparator<T> & Serializable)
            (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2));
    }

Among them, return (Comparator<T> & Serializable) should represent type forced conversion, but why not convert it directly to Comparator<T> instead of using the logical AND symbol? ?

ringa_leeringa_lee2687 days ago693

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-17 10:00:41

    This means forced conversion to Comparator<T>Serializable
    You can read this explanation

    reply
    0
  • Cancelreply