java自动装箱自动拆箱==记录

版权声明:转载博文请注明出处。 https://blog.csdn.net/weixin_41297332/article/details/83011916
kage com.daxue;

public class Test {
	public static void main(String[] args) {
		//①自动装箱
		Integer a = 10;
		Integer b = 10;
		System.out.println(a==b);
		
		//②
		Integer aInteger = 1000;
		Integer bInteger = 1000;
		System.out.println(aInteger==bInteger);
        }
}

/*由①和②可知:
         * 根据自动装箱的规则
         * Integer intObj = 1 <==> Integer intObj = Integer.valueOf(1);
         * valueOf方法源码:
         * public static Integer valueOf(int i){
         *         assert IntegerCache.high >= 127;
         *         if(i >= IntegerCache.low && i <= IntegerCache.high){
         *             return IntergeCache.cache[i+(-IntegerCache.low)]
         *         }
         *        return new Integer(i);
         * }
         *
         * 由上可知:i在[-128,127]范围内,直接从缓存中取出一个事先new好的对象返回,即返回缓存中的对象
         * 因为超出范围,new了一个新的对象,者就是==号不成立的原因
         */

//自动拆箱: Integer和int类型进行 == > >= < <=比较时,会把Integer自动拆箱

//Integer和Integer进行 > >= < <=比较时,两个都会自动拆箱

猜你喜欢

转载自blog.csdn.net/weixin_41297332/article/details/83011916