最近在读lambda表达式相关的java源码,在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));
    }
其中return (Comparator<T> & Serializable)应该是表示类型强转,但是这里为什么不直接转换成Comparator<T>,而要用逻辑与符号?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这个表示强转成
Comparator<T>和Serializable可以看下这个解释