Integer的计算比较

请判断如下题目的输出结果

 

public class IntegerTest {
	public static void main(String[] args) {
		Integer a=100, b = 100, c=500, d=500;
		System.out.println(a == b);
		System.out.println(c == d);
	}
}


你可能会认为要么全为true,要么全为false;也可能你确实对这些东西了解过了,认为第一个为true,第二个为false。但是你清楚为什么吗?其实全不对,这个题目的结果是不确定的

 

 

1.首先我们先来了解一下Integer与int的关系

 

 1.1java中四类八种基本数据类型,以及封装类型

数值型:byteshort int long

字符型:char

浮点型:floatdouble

布尔型:boolean

 

基本数据类型byte(字节型)short(短整型))int(整型)long(长整型)float(浮点型)double(双精度)char(字符型)boolean(布尔型)
占用字节1248482系统没有提供size方法 
默认值0000.0l0.0f0.0/u0000(空格)false
封装器类ByteShortIntegerLongFloatDoubleCharacterBoolean

 

  1.2 区分基本类型与封装类型的好处

 

 网上有很多说明,这里就不再赘述了

 

2.数值比较为什么不确定?

 

2.1 赋值f1 = 100时,java内部发生了什么

 

/**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} 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.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;// vm配置最大值不能小于127
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    } assert IntegerCache.high >= 127;// vm配置最大值不能小于127
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

 

 

/**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the -XX:AutoBoxCacheMax=<size> option.   通过该参数调整最大值high
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low));
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }-XX:AutoBoxCacheMax=<size> option.   通过该参数调整最大值high
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */
private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                int i = parseInt(integerCacheHighPropValue);
                i = Math.max(i, 127);
                // Maximum array size is Integer.MAX_VALUE
                h = Math.min(i, Integer.MAX_VALUE - (-low));
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

        private IntegerCache() {}
    }


     从上面可以了解到,树智的比较与IntegerCache的low与high相关,而low为默认值-128,high默认为127;所以有一个说法当integer 取值范围-128至127之间时由于是通过 cache[]中获取所以比较值应该为true;

 

    但是文章内部也会看到 最大值其实是可以变化的,通过如下信息获得,也就是与vm配置相关,但同时这个配置值不能小于127

<span style="color:#ff0000">-XX:AutoBoxCacheMax=<size></span>
java.lang.Integer.IntegerCache.high

 

3.总结

   Integer -128至127之间的数值比较结果:true

                 小于-128之间的树智比较结果:false

                  大于127之间的树智比较结果:不确定,与虚拟机配置参数有关

 

4.趣味思考

  思考一个问题: Integer a = 200; int b = 200; a==b?

  结果:相等,考虑下为什么?

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值