• Java 之Integer相等比较


    1.问题提出

      今天在和同事讨论问题的时候,无意间谈到了Integer对象的比较,先看下代码:

    package test;
    
    
    public class IntegerEqual {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		Integer a = 1;
    		Integer b = 1;
    		Integer c = 2000;
    		Integer d = 2000;
    		
    		
    		System.out.println(a==b);
    		System.out.println(c==d);
    	
    	}
    
    }
    

      相信很多人一看到这段代码,就会感觉输出的结果是true,true;实际上这段代码的输出结果是true,false;这是为什么呢?同样的对象为什么比较的结果不一样呢?

    2.结果分析

      先来分析下Integer a=1;的实现方式,1是数字是怎么放到Integer这个对象中去的;我们知道这是JDK5新增的语法自动装箱和拆箱功能实现的,可是具体的这个装箱功能室如何实现的呢?我们先来看下Integer.valueOf()这个方法,因为这是Integer默认的装箱的实现方式:

     /**
         * Returns a <tt>Integer</tt> instance representing the specified
         * <tt>int</tt> value.
         * If a new <tt>Integer</tt> instance is not required, this method
         * should generally be used in preference to the constructor
         * {@link #Integer(int)}, as this method is likely to yield
         * significantly better space and time performance by caching
         * frequently requested values.
         *
         * @param  i an <code>int</code> value.
         * @return a <tt>Integer</tt> instance representing <tt>i</tt>.
         * @since  1.5
         */
        public static Integer valueOf(int i) {
            if(i >= -128 && i <= IntegerCache.high)
                return IntegerCache.cache[i + 128];
            else
                return new Integer(i);
        }
    

      通过注释也可以发现,这是JDK5的时候新增的方法,先来简单分析下这个方法具体做了什么事情;原来她内部维护了一个缓存池,它总是Integer缓存池中获取Integer对象,超出了其缓存池缓存池(-128到127),它才new新的Integer对象。只要在这个范围内,都是直接取出对象而不是创建,所以值总是相等的,但是如果超过了这个范围,那么它就会穿件新的对象(-2147483648到-128和127到2147483647)(Integer.MAX_VALUE:2147483647,Integer.MIN_VALUE:-2147483648),一旦是创建的对象,那么比较的是对象自然是不相等,即使值是相等的。

    3.其他

       public boolean equals(Object obj) {
    	if (obj instanceof Integer) {
    	    return value == ((Integer)obj).intValue();
    	}
    	return false;
        }
    

      因为这个方法取得是具体的值,所以不管是多少,具体的结果总是相等的。

  • 相关阅读:
    python 内存泄漏——使用pymssql模块的讨论 free(): corrupted unsorted chunks
    Python的gc模块
    使用多线程——线程池
    sqlserver 数据库连接池
    drf response——简单封装
    邮箱找回密码实现
    阿里云 oss 服务 —— 上传图片,获取url
    dajngo-apscheduler 实现定时任务
    kubernetes基础概念
    Path must be a string.
  • 原文地址:https://www.cnblogs.com/zhangminghui/p/4487710.html
Copyright © 2020-2023  润新知