zoukankan      html  css  js  c++  java
  • Integer比较

    这个Integer比较真的是坑啊..........

    先看代码,大家来猜猜,看看到底会输出什么:

     1 public class Test1 {
     2 
     3     public static void main(String[] args) {
     4         Integer a = 128;
     5         Integer b = 128;
     6         int c = 128;
     7         System.out.println(a==b);
     8         System.out.println(b==c);
     9         System.out.println(c==b);
    10         
    11         System.out.println("=========");
    12         Integer a1 = 10;
    13         Integer b1 = 10;
    14         int c1 = 10;
    15         System.out.println(a1==b1);
    16         System.out.println(b1==c1);
    17         System.out.println(c1==b1);
    18         System.out.println("=========");
    19         
    20         int a11 = 10;
    21         Integer b11 = new Integer(10);
    22         System.out.println(a11== b11);     //Line 1
    23 
    24         Integer a2 = 127;
    25         Integer b2 = 127;
    26         System.out.println(a2==b2);     //Line 2
    27 
    28         Integer a3 = 128;
    29         Integer b3 = 128;
    30         System.out.println(a3 == b3);   //Line 3
    31 
    32         Integer a4 = new Integer(10);
    33         Integer b4 = new Integer(10);
    34         System.out.println(a4==b4);     //Line 4
    35         
    36     }
    37 
    38 }

    结果如下:

    false
    true
    true
    =========
    true
    true
    true
    =========
    true     //line 1
    true     //line 2
    false    //line 3
    false    //line 4

    不知猜对几道???笑哭...

    先说下我的错误思路!!!

    之前我是认为:Integer的话,不论范围多少,都是一个new 了一个对象,然后,不同的对象肯定地址也不相同,当==的时候,总是false;

    而由int-->integer自动装箱时,如果是像第8行那样,也是两个不同对象,但像第9行那样,就成了拆箱成int ,比较的就是数值大小。

    现在发现,这完完全全就是错误的!!!!!!!

    哭晕到厕所.....

    等会儿,容我先解释解释再去/...

    查了些资料,直接看源码吧,看看==这个操作到底是怎么实现的。

         /* This method will always cache values in the range -128 to 127,
         * inclusive, and may cache other values outside of this range.
         */    
       public static Integer valueOf(int i) {
            assert IntegerCache.high >= 127;
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];//返回缓存内的对象
            return new Integer(i);//如果不在区间内,则新返回一个new对象;
        }

    看到了没!!!

    这里low是-128high是127,从方法的注释中可以得知:

    [-128,127]之间,Integer总是缓存了相应数值对象,这样每次取值时都是同一个对象

    而不在此区间时,则不一定会缓存,那么取出两个同样数值大小的对象则不是同一个对象,每次都会new Integer(i);

    所以其对象的地址不同,则用==比较是自然不会成立。

    另外需要注意的是,若是直接new Integer(n),则不管n是否在[-128, 127]范围内,都是不同的对象,而当没有显示new 出对象时,则利用缓存里的对象,此时是同一个对象(都是缓存内的那个)。

    还有就是上述Integer与int之间的比较,可以四种:

    • 1) Integer与int类型的赋值

                    a.把int类型赋值给Integer类型。此时,int类型变量的值会自动装箱成Integer类型,然后赋给Integer类型的引用,这里底层就是通过调用valueOf()这个方法来实现所谓的装箱的。
                    b.把Integer类型赋值给int类型。此时,Integer类型变量的值会自动拆箱成int类型,然后赋给int类型的变量,这里底层则是通过调用intValue()方法来实现所谓的拆箱的。

    •  2) Integer与int类型的比较

                    这里就无所谓是谁与谁比较了,Integer == int与int == Integer的效果是一样的,都会把Integer类型变量拆箱成int类型,然后进行比较,相等则返回true,否则返回false。同样,拆箱调用的还是intValue()方法。

    •  3) Integer之间的比较

                    这个就相对简单了,直接把两个引用的值(即是存储目标数据的那个地址)进行比较就行了,不用再拆箱、装箱什么的。

    • 4) int之间的比较

                    这个也一样,直接把两个变量的进行比较

    Over....

    参考:

    1.Integer 大小比较遇到过的坑

    2.踩坑笔记之Integer数值比较

    3.Java中Integer与int类型的装箱和拆箱

  • 相关阅读:
    iOS中使用nil NULL NSNULL的区别
    Xcode常用快捷键总结
    (求租仓库)navigationController .navigationBar 的属性设置
    imageNamed 与 initWithContentsOfFile 区别
    iOS-Senior10-多线程(子线程创建)
    iOS-setValue和setObject的区别
    iOS-Senior8-网络之进阶
    iOS-Senior7-数据请求
    iOS-Senior6-数据解析(JSON)
    iOS-Senior6-数据解析(XML)
  • 原文地址:https://www.cnblogs.com/gjmhome/p/11530366.html
Copyright © 2011-2022 走看看