Integer大小的比较

来源:互联网 发布:windows 10 mobile壁纸 编辑:程序博客网 时间:2024/05/20 11:36

原文地址:http://angelinafan.blog.163.com/blog/static/1361727982011224112444751/

今天发现一个挺诡异的问题,先来看看下面这段代码:


view plaincopy to clipboardprint?
public class Test {  
    public static void main(String[] args) {  
        Integer a = 1000;  
        Integer b = 1000;  
        System.out.println("a==b : " + (a == b));  
        System.out.println("a.equals(b) : " + a.equals(b));  
    }  
}  

这段代码的输出结果是这样的:


view plaincopy to clipboardprint?
a==b : false  
a.equals(b) : true  

对于基本类型,以及基本类型的比较,我一直都是用“==”来进行比较的。一直没注意这个问题,今天遇到了吓一大跳。庆幸过去的程序中用“==”没出现问题。把源码拖出来看了一下,明白了,解释如下:

平常我们使用Integer a = xxx;的时候,Integer类实际上是调用的是public static Integer valueOf(int i)方法。这个方法是这样的。


view plaincopy to clipboardprint?
   public static Integer valueOf(int i) {  
final int offset = 128;  
if (i >= -128 && i <= 127) { // must cache   
    return IntegerCache.cache[i + offset];  
}  
       return new Integer(i);  
   }  

从上面代码可以看出,当进行赋值操作时,Java将这个值分为两个区间来处理,即:

i 属于[-128, 127]的时,返回IntegerCache.cache[i + offset];

i 属于上面范围以外时,返回new Integer(i)。

上面实例程序中,赋值超出[-128, 127]返回,所以返回为new Integer(1000),而“==”比较是比较内存地址,那么返回值肯定为FALSE。

至此上面陈述的诡异问题就解开了。

但是Java为什么要这样分区间处理呢,这不是使劲儿把我们往误区里勾引吗?为此我测试了这样一个程序:赋值在[-128, 127]之间时。

 


view plaincopy to clipboardprint?
public class Test {  
    public static void main(String[] args) {  
        Integer a = 10;  
        Integer b = 10;  
        System.out.println("a==b : " + (a == b));  
        System.out.println("a.equals(b) : " + a.equals(b));  
    }  
}  

 这段代码的输出结果是这样的:


view plaincopy to clipboardprint?
a==b : true  
a.equals(b) : true  

为什么出现这种现象呢?这就要问IntegerCache.cache[i + offset]了,跟踪进去代码如下:


view plaincopy to clipboardprint?
static final Integer cache[] = new Integer[-(-128) + 127 + 1];  
  
static {  
    for(int i = 0; i < cache.length; i++)  
    cache[i] = new Integer(i - 128);  
}  

也就是说,当赋值在[-128, 127]区间时,Java是从同一个数据中取出同一个对象,即内存地址一样,所以“==”操作返回TRUE;

那么猜测Java这样做的目的,可能[-128, 127]区间比较常用,这个区间内的Integer对象也作为静态变量初始化完成,这样直接返回对象可以提高效率。

为了防止不小心掉入这样的陷阱,对于基本类型的比较,用“==”;而对于基本类型的封装类型和其他对象,应该调用public boolean equals(Object obj)方法(复杂对象需要自己实现equals方法)。

0 0
原创粉丝点击