Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining return type via bitwise operation

Tags:

java

generics

I was wandering throught java docs and suddenly found this code:

public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
        Function<? super T, ? extends U> keyExtractor)
{
    Objects.requireNonNull(keyExtractor);
    return (Comparator<T> & Serializable)
        (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}

Can anyone explain what kind of magic is going on after the return statement? Okay, the result of the method is determined by lambda expression in combination with functional interface. But what is it written before that? Is it casting return type via bitwise operation? I don't get it. As far I know bitwise is applicable only with numbers. Where can I read about this case more specifically?

like image 948
Lysenko Andrii Avatar asked May 02 '26 15:05

Lysenko Andrii


1 Answers

Java 8 adds the ability to cast the lambda into an anonymous intersection. The return type is both a Comparator<T> and Serializable

Referenced from: assylias's answer here

Documentation

NOTE: Java sometimes adds new syntactic meanings to operators in order to maintain backwards compatibility.

like image 111
cyroxis Avatar answered May 05 '26 03:05

cyroxis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!