美文网首页
第 61 条:基本类型优先于装箱类型

第 61 条:基本类型优先于装箱类型

作者: 综合楼 | 来源:发表于2021-06-08 20:00 被阅读0次
基本类型优先于装箱类型.jpeg
// Broken comparator - can you spot the flaw?
Comparator<Integer> naturalOrder = (i, j) -> (i < j) ? -1 : (i == j ? 0 : 1);

Comparator<Integer> naturalOrder = (iBoxed, jBoxed) -> {
    int i = iBoxed, j = jBoxed; // Auto-unboxing
    return i < j ? -1 : (i == j ? 0 : 1);
};
public class Unbelievable {
    static Integer i;
    public static void main(String[] args) {
        if (i == 42)
            System.out.println("Unbelievable");
    }
}
// Hideously slow program! Can you spot the object creation?
public static void main(String[] args) {
    Long sum = 0L;
    for (long i = 0; i < Integer.MAX_VALUE; i++) {
        sum += i;
    }
    System.out.println(sum);
}

相关文章

网友评论

      本文标题:第 61 条:基本类型优先于装箱类型

      本文链接:https://www.haomeiwen.com/subject/zevyjltx.html