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

Java 1.5增加自动装箱和自动拆箱,对应基本类型int、double、boolean,装箱基本类型是Integer、Double、Boolean。这两种类型之间差别。

 

基本类型和装箱基本类型之间的三个主要区别:

1.基本类型只有值,而装箱基本类型具有与它们的值不同的同一性(两个装箱基本类型可以具有相同的值和不同的同一性)

2.基本类型只有功能完备的值,而每个装箱基本类型除了它对应的基本类型的所有功能值之外,还有个非功能值:null

3.基本类型通常比装箱基本类型更节省空间和时间。

 

对于三个主要区别分别的例子:

1.考虑

Comparator<Integer> naturalOrder = new Comparator<Integer>() {
    public int compare(Integer first, Integer second) {
        return first < second ? -1 : (first == second ? 0 : 1);
    }
};

如果打印

naturalOrder.Compare(new Integer(42), new Integer(42));

的值,结果不是期望的0,而是1,表明第一个Integer值大于第二个。

问题出现在执行first == second,它在两个对象引用上执行同一性比较,如果first和second引用表示同一个int值得不同Integer实例,这个比较操作会返回false,比较器会错误地返回1,对装箱基本类型运用==操作符几乎总是错误的。

修正的方法是添加两个局部变量,保存对应于first和second的基本类型int值,并在这些变量上执行所有的比较操作:

Comparator<Integer> naturalOrder = new Comparator<Integer>() {
    public int compare(Integer first, Integer second) {
    int f = first;
        int s  = second;
        return f < s ? -1 : (f == s ? 0 : 1);
    }
};

 

2.考虑

public class Unbelievable {
    static Integer i;
    public static void main(String[] args) {
        if(42 == i)
            System.out.println("Unbelievable");
    }
}

它不是打印出Unbelievable,而是抛出NullPointException异常,问题在于i是个Integer,不是int,它的初始值是null而不是0,当计算(i == 42)时,将Integer和int进行比较,装箱基本类型自动拆箱,如果null对象被自动拆箱,就只能得到Nu'llPointException了。

修正的方法把i声明为int。

 

3.考虑

public static void main(String[] args) {
    Long sum = 0L;
    for(long i = 0; i < Integer.MAX_VALUE; i++) {
        sum += i;
    }
    System.out.println(sum);
}

程序的运行比较慢,原因在于把局部变量sum声明为装箱基本类型Long,而不是基本类型long,程序运行完全没问题,只是会导致明显的性能下降,因为变量被反复地进行装箱和拆箱。

 

适合使用装箱基本类型的地方:

1.作为集合中的元素,键和值。

2.在参数化类型中,必须使用装箱基本类型作为类型参数。

3.在进行反射方法调用时,必须使用装箱基本类型。

posted @ 2016-08-16 14:52  没有梦想的小灰灰  阅读(500)  评论(0编辑  收藏  举报