芝麻_糊

导航

基本类型优先于装箱基本类型

 基本类型与包装类型的主要区别在于以下三个方面:

1、基本类型只有值,而包装类型则具有与它们的值不同的同一性(identity)。这个同一性是指,两个引用是否指向同一个对象,如果指向同一个对象,则说明具有同一性。(与此类似的还有等同性。)

2、基本类型只有功能完备的值,而包装类型除了其对应的基本类型所有的功能之外,还有一个非功能值:null。

3、基本类型通常比包装类型更节省时间与空间。

 1 package com;
 2 
 3 import java.util.Comparator;
 4 
 5 /**
 6  * Created by huyanxia on 2017/11/27.
 7  */
 8 public class TestInteger {
 9     public static Integer i;
10 
11     public static void main(String[] args) {
12         Integer a = i;
13         //int j = i;
14         /* 1.因为Integer转换为int时,会首先进行自动拆箱,但是若为null,就会出现空指针异常*/
15         //输出null,Integer默认为null
16         System.out.println("输出" + a);
17         //java.lang.NullPointerException
18         System.out.println("输出" + i);
19         /* 2.因为执行first < second 时,会自动拆箱,再执行first == second时会执行同一性(引用是否指向同一个对象,若是为true)
20         比较,即first和second引用表示同一个int 值的不同的Interger实例,那么就会返回false,比较器就会错误的返回1*/
21         //大小1;
22         System.out.println("大小" + order.compare(new Integer(42), new Integer(42)));
23         //大小0
24         System.out.println("大小" + order.compare(42, 42));
25 
26         /* 3.Integer的常量池是由-128至127组成。当我们给一个Integer赋的值在这个范围之类时就直接会从缓存返回一个相同的引用,
27          所以m1 == n1,m3 == n3会输出true。而超过这个范围时,就会重新new一个对象。因此,m == n,m4 == n4就会输出一个false。*/
28         // false
29         Integer m = 980;
30         Integer n = 980;
31         System.out.println("结果:" + (m == n));
32         //true
33         Integer m1 = 127;
34         Integer n1 = 127;
35         System.out.println("结果:" + (m1 == n1));
36         //false
37         Integer m2 = 128;
38         Integer n2 = 128;
39         System.out.println("结果:" + (m2 == n2));
40         //true
41         Integer m3 = -128;
42         Integer n3 = -128;
43         System.out.println("结果:" + (m3 == n3));
44         //false
45         Integer m4 = -129;
46         Integer n4 = -129;
47         System.out.println("结果:" + (m4 == n4));
48         /* 4.基本类型只有值,而包装类型则具有与它们的值不同的同一性(identity)。这个同一性是指,两个引用是否指向同一个对象,
49         如果指向同一个对象,则说明具有同一性。(与此类似的还有等同性。)*/
50         //false
51         Integer m5 = new Integer(127);
52         Integer n5 = new Integer(127);
53         System.out.println("结果:" + (m5 == n5));
54 
55         /* 5.基本类型通常比包装类型更节省时间与空间。因为,在声明sum变量的时候,一不小心声明为Long,
56           而不是long。这样,在这个循环当中就会不断地装箱和拆箱,其性能也会明显的下降。但是,将Long改成long时间消耗会缩短很多*/
57         //4999950000,时间:14ms
58         long startTime = System.currentTimeMillis();
59         Long sum = 0L;
60         for(long i = 0;i < 100000; i++){
61             sum +=i;
62         }
63         System.out.println(sum + ",时间:" + (System.currentTimeMillis() - startTime) + "ms");
64 
65         //4999950000,时间:3ms
66         long startTime1 = System.currentTimeMillis();
67         long sum1 = 0;
68         for(long i = 0;i < 100000;i++){
69             sum1 +=i;
70         }
71         System.out.println(sum1 + ",时间:" + (System.currentTimeMillis() - startTime1) + "ms");
72         
73        /* 因为int的最大值为 2147483647,而累加超过 2147483647,就会变成负数,所以int的累加结果小*/
74         //704982704,时间:7ms
75         long startTime2 = System.currentTimeMillis();
76         Integer sum2 = 0;
77         for(int i = 0;i < 100000;i++){
78             sum2 +=i;
79         }
80         System.out.println(sum2 + ",时间:" + (System.currentTimeMillis() - startTime2) + "ms");
81 
82         //704982704,时间:2ms
83         long startTime3 = System.currentTimeMillis();
84         int sum3 = 0;
85         for(int i = 0;i < 100000;i++){
86             sum3 +=i;
87         }
88         System.out.println(sum3 + ",时间:" + (System.currentTimeMillis() - startTime3) + "ms");
89     }
90     static Comparator<Integer> order = new Comparator<Integer>() {
91         @Override
92         public int compare(Integer first, Integer second) {
93             return first < second ? -1 : (first == second ? 0 : 1);
94         }
95     };
96 }

 

适合包装类型的三个情况:

1、作为集合中的元素、键和值。
2、在参数化类型中。比如:你不能这样写——ArryList<int>,你只能写ArrayList<Integer>.
3、在进行反射方法的调用时。

posted on 2017-11-27 16:29  芝麻_糊  阅读(463)  评论(0)    收藏  举报