zoukankan      html  css  js  c++  java
  • Integer类型与int的==比较

    前言

    Java中有两种类型

    • 基本类型

    基本数据类类型存的是数值本身

    • 引用类型

    引用类型变量在内存放的是数据的引用

    基本类型通过==比较的是他们的值大小,而引用类型比较的是他们的引用地址

    正文

    在一些特殊的类中,如Integer在使用==比较的时候往往非常容易出错,下面通过几个例子来探索一下具体的原理

    代码片段

    public class Test03 {
    
        public static void main(String[] args) {
            Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
    
            System. out.println( f1 == f2); //true
            System. out.println( f3 == f4); //false
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    当我们给一个Integer赋予一个int类型的时候会调用Integer的静态方法valueOf。 
    Integer f1 = Integer.valueOf(100); 
    Integer f2 = Integer.valueOf(100); 
    Integer f3 = Integer.valueOf(150); 
    Integer f4 = Integer.valueOf(150); 
    思考:那么Integer.valueOf()返回的Integer是不是是重新new Integer(num);来创建的呢?如果是这样的话,那么== 比较返回都是false,因为他们引用的堆地址不一样。

    具体来看看Integer.valueOf的源码

    public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    在IntegerCache中cache数组初始化如下,存入了-128 - 127的值

    cache = new Integer[(high - low) + 1];
    int j = low;
    for( int k = 0; k < cache.length ; k ++)
        cache[k] = new Integer(j ++);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    从上面我们可以知道给Interger 赋予的int数值在-128 - 127的时候,直接从cache中获取,这些cache引用对Integer对象地址是不变的,但是不在这个范围内的数字,则new Integer(i) 这个地址是新的地址,不可能一样的

    代码片段

    public static void main(String[] args) {
        Integer a = new Integer(3);
        Integer b = 3;                 
        int c = 3;
        System.out.println(a == b);    
        System.out.println(a == c);    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    a == b分析 
    Integer b = 3; 自动调用Integer.valueOf(3) 返回一个Integer的对象。 这个对象存放到cache中的(上面一段代码分析)。 而 Integer a = new Integer(3);这里创建了一个新的对象Integer 所以 a == b 返回的是false

    a == c 分析 
    一个Integer 与 int比较,先将Integer转换成int类型,再做值比较,所以返回的是true。

    参考资料:《探索java基本类型和包装类型的使用运算符==进行比较的底层细节》

    延伸

    java中还有与Integer类似的是Long,它也有一个缓存,在区间[-128,127]范围内获取缓存的值,而Long与long比较的时候先转换成long类型再做值的比较

    Double类型,它没有缓存,但是当Double与double比较的时候会先转换成double类型,再做值的比较

    源地址:http://blog.csdn.net/sgls652709/article/details/49079767


  • 相关阅读:
    Insert Buffering
    B-Tree vs LSM-tree
    MySQL 5.6 死锁演示 及 日志分析
    MySQL索引
    InnoDB的三个关键特性
    MySQL如何优化GROUP BY :松散索引扫描 VS 紧凑索引扫描
    MySql 自适应哈希索引
    母牛的故事
    简单的java程序
    一些变量的比较
  • 原文地址:https://www.cnblogs.com/chz-blogs/p/9381021.html
Copyright © 2011-2022 走看看