java基础Integer

java基础Integer

int装箱源码
/**
     * The value of the {@code Integer}.
     *
     * @serial
     */
    private final int value;

    public Integer(int value) {
        this.value = value;
    }
    
    ...
    
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
  
    ...
    
	 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) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

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

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

由以上源码可知[ -128,127]区间的int对象装箱时会取常量池缓存对象

int拆箱源码
    /**
     * Returns the value of this {@code Integer} as an
     * {@code int}.
     */
    public int intValue() {
        return value;
    }

"=="与"equals"区别

1、equals

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

由以上源码可知equals是拆箱后两个int值进行比较

2、“==”
比较的是值和对象应用地址

实验
 public static void main(String[] paramArrayOfString) {
        Integer i1 = 7;
        Integer i2 = 7;
        Integer i3 = new Integer(7);
        Integer i4 = new Integer(7);
        Integer i5 = new Integer(14);
        Integer i6 = 14;
        Integer i7 = 128;
        Integer i8 = 128;
        Integer i9 = -129;
        Integer i10 = -129;

        System.out.println("i1                                              " + System.identityHashCode(i1));
        System.out.println("i2                                              " + System.identityHashCode(i2));
        System.out.println("i3                                              " + System.identityHashCode(i3));
        System.out.println("i4                                              " + System.identityHashCode(i4));
        System.out.println("i5                                              " + System.identityHashCode(i5));
        System.out.println("i6                                              " + System.identityHashCode(i6));
        System.out.println("i3+i4                                           " + System.identityHashCode(i3 + i4));
        System.out.println("i1+i2                                           " + System.identityHashCode(i1 + i2));

        System.out.println("7.equals(7)                                     " + (i1.equals(i2)));//比较值
        System.out.println("7.equals(new Integer(7))                        " + (i2.equals(i3)));//比较值
        System.out.println("7==7                                            " + (i1 == i2));//比较对象 值和引用地址
        System.out.println("7==new Integer(7)                               " + (i2 == i3));//比较对象 值和引用地址
        System.out.println("7+7==new Integer(14)                            " + (i1 + i2 == i5));//比较值
        System.out.println("7+7==14                                         " + (i1 + i2 == i6));//比较值
        System.out.println("new Integer(7)+new Integer(7)==new Integer(14)  " + (i3 + i4 == i5));//比较值
        System.out.println("new Integer(7)+new Integer(7)==14               " + (i3 + i4 == i6));//比较值
        System.out.println("128==128                                        " + (i7 == i8));//比较对象 值和引用地址
        System.out.println("-129==-129                                      " + (i9 == i10));//比较对象 值和引用地址
    }
i1                                              705927765
i2                                              705927765
i3                                              366712642
i4                                              1829164700
i5                                              2018699554
i6                                              1311053135
i3+i4                                           1311053135
i1+i2                                           1311053135
7.equals(7)                                     true
7.equals(new Integer(7))                        true
7==7                                            true
7==new Integer(7)                               false
7+7==new Integer(14)                            true
7+7==14                                         true
new Integer(7)+new Integer(7)==new Integer(14)  true
new Integer(7)+new Integer(7)==14               true
128==128                                        false
-129==-129                                      false

结果解析:
1、因equals比较的是int值,所以i1.equals(i2)和i2.equals(i3)为真;
2、i1,i2属于常量池中缓存对象,所以引用地址相同,又因值相等 所以i1 == i2为真;
3、i3为新建的对象,不属于常量池中缓存对象,所以引用地址不相同,因而i2==i3为假;
4、因Integer对象无法直接参与运算,所以需要对其进行拆箱后再行运算,所以i1 + i2 == i5、i1 + i2 == i6、i3 + i4 == i5及i3 + i4 == i6都是先进行拆箱变成int对象进行运算后再行比较,实际比较的是运算后的值,因而为真;
5、i7,i8,i9,i10在[-128,127]缓存范围之外都是new出来的新对象,所以i7 == i8和i9 == i10为假;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值